NewMessageNotification 它不知道有没有做什么特别的处理
目前我的执行没有任何报错,私信发送成功,但关于这个私信的消息提示一直没有.
代码大概如下:
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Notifications\Notification;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
class NewMessageNotification extends Notification
{
use Queueable;
public $message;
public function __construct(Message $message)
{
$this->message = $message;
}
public function via($notifiable)
{
// return ['mail'];
return ['database'];
}
public function toDatabase($notifiable)
{
return [
'name' => $this->message->fromUser->name,
'dialog' => $this->message->dialog_id,
];
}
<?php
namespace App\Http\Controllers\Home;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repositories\MessageRepository;
use App\Notifications\NewMessageNotification;
use Illuminate\Support\Facades\Notification;
use Auth;
class InboxController extends Controller
{
protected $message;
public function __construct(MessageRepository $message)
{
$this->middleware('auth');
$this->message = $message;
}
/**
* 私信首页
*/
public function index()
{
$messages = $this->message->getAllMessages();
return view('inbox.index',['messages'=>$messages->unique('dialog_id')->groupBy('dialog_id')]);
}
/**
* 显示私信列表
*/
public function show($dialogId)
{
$messages = $this->message->getMessageByDialogId($dialogId);
$messages->markAsRead();
return view('inbox.show', compact('messages', 'dialogId'));
}
/**
* 私信回复
*/
public function store($dialogId)
{
// 根据 $dialogId 查询用户私信相关内容
$message = $this->message->getSingleMessageBy($dialogId);
// 获取接收私信用户的id
$toUserId = $message->from_user_id == user()->id ? $message->to_user_id : $message->from_user_id;
$newMessage = $this->message->addMessage([
'from_user_id' => user()->id,
'to_user_id' => $toUserId,
'body' => request('body'),
'dialog_id' => $dialogId
]);
// 这里希望接收私信用户看到消息提示,所以用 toUser
$newMessage->toUser->notify(new NewMessageNotification( $newMessage ));
return back();
}
}
报错内容如下:
Type error: Argument 1 passed to App\Notifications\NewMessageNotification::__construct() must be an instance of App\Notifications\Message, instance of App\models\Message given, called in /vagrant/zt360/admin/app/Http/Controllers/Home/InboxController.php on line 62
问题解决,没有引入 model