错误 enter image description here
当有人喜欢和评论他的帖子时,我正在尝试发送活动通知,喜欢和喜欢工作的通知
这是我的通知课。
我的CommentController if ($event->user_id != $comment->user_id)
class NewCommentEvent extends Notification { use Queueable; protected $comment; /** * Create a new notification instance. * * @return void */ public function __construct($comment) { $this->comment = $comment; } /** * Get the notification's delivery channels. * * @param mixed $notifiable * @return array */ public function via($notifiable) { return ['database']; } /** * Get the array representation of the notification. * * @param mixed $notifiable * @return array */ public function toDatabase($notifiable) { return [ 'comment' => $this->comment, 'event' => Event::find($this->comment->event_id), 'user' => User::find($this->comment->user_id) ]; } /** * Get the array representation of the notification. * * @param mixed $notifiable * @return array */ public function toArray($notifiable) { return [ // ]; } }
我的用于通知通知的控制器功能代码
public function store(CommentRequest $request) { $event = Event::findOrFail($request->event_id); Comment::create([ 'comment' => $request->comment, 'user_id' => Auth::id(), 'event_id' => $event->id ]); if ($event->user_id != $comment->user_id) { $user = User::find($event->user_id); $user->notify(new NewCommentEvent($comment)); } Toastr::success('Comment post with success','', ["positionClass" => "toast-top-center"]); return redirect()->back(); }
我的CommenRequest
namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; use Illuminate\Support\Facades\Auth; class CommentRequest extends FormRequest { /** * Determine if the user is authorized to make this request. * * @return bool */ public function authorize() { return Auth::check(); } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'comment' => 'required|max:2000', ]; } }
答案 0 :(得分:1)
在您的控制器中:未定义变量$comment
。
来自Laravel文档:
create
方法返回保存的模型实例。
因此解决方案是:
$comment = Comment::create([
'comment' => $request->comment,
'user_id' => Auth::id(),
'event_id' => $event->id
]);
答案 1 :(得分:0)
您尚未定义$ comment,您只是创建了一条注释。这引发了错误
$comment = Comment::create([
.
.
]);
这将解决您的问题
答案 2 :(得分:0)
错误消息已清除。 $comment
未定义。用以下代码替换您的控制器代码:
public function store(CommentRequest $request)
{
$event = Event::findOrFail($request->event_id);
// defined comment here
$comment = Comment::create([
'comment' => $request->comment,
'user_id' => Auth::id(),
'event_id' => $event->id
]);
if ($event->user_id != $comment->user_id) {
$user = User::find($event->user_id);
$user->notify(new NewCommentEvent($comment));
}
Toastr::success('Comment post with success','', ["positionClass" => "toast-top-center"]);
return redirect()->back();
}