评论通知不适用于laravel

问题描述 投票:1回答:3

错误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',
        ];
    }
}
php laravel notifications laravel-notification
3个回答
© www.soinside.com 2019 - 2024. All rights reserved.