laravel 通知未存储到我的数据库中加上错误
laravel notifications not storing into my database plus error
我无法将通知存储到我的数据库中的通知 table 中,
我试图在每次有新的 Post 时发出通知,我怎样才能使这项工作正常进行。
错误:
通知:
use Queueable;
public $post;
public function __construct()
{
}
public function via($notifiable)
{
return ['database'];
}
public function toDatabase($notifiable)
{
return [
'title' => $this->post->title,
];
}
public function toArray($notifiable)
{
return [
];
}
}
Post 控制器:
public function store(Request $request)
{
$this->validate($request, [
'title'=>'required|max:100',
'body' =>'required',
]);
$title = $request['title'];
$body = $request['body'];
$post = Post::create($request->only('title', 'body'));
Auth::user()->notify(new NotifyPost($post));
return redirect()->route('posts.index')
->with('flash_message', 'Article,
'. $post->title.' created');
}
您需要在构造中注入 post,否则它永远无法解析。
protected $post;
public function __construct(Post $post)
{
$this->post = $post;
}
我无法将通知存储到我的数据库中的通知 table 中, 我试图在每次有新的 Post 时发出通知,我怎样才能使这项工作正常进行。
错误:
通知:
use Queueable;
public $post;
public function __construct()
{
}
public function via($notifiable)
{
return ['database'];
}
public function toDatabase($notifiable)
{
return [
'title' => $this->post->title,
];
}
public function toArray($notifiable)
{
return [
];
}
}
Post 控制器:
public function store(Request $request)
{
$this->validate($request, [
'title'=>'required|max:100',
'body' =>'required',
]);
$title = $request['title'];
$body = $request['body'];
$post = Post::create($request->only('title', 'body'));
Auth::user()->notify(new NotifyPost($post));
return redirect()->route('posts.index')
->with('flash_message', 'Article,
'. $post->title.' created');
}
您需要在构造中注入 post,否则它永远无法解析。
protected $post;
public function __construct(Post $post)
{
$this->post = $post;
}