src/CommentsController.php
public function store(Request $request)
    {
        $this->authorize('create-comment', Comment::class);
        
        $this->validate($request, [
            'commentable_type' => 'required|string',
            'commentable_id' => 'required|integer|min:1',
            'message' => 'required|string'
        ]);

        $model = $request->commentable_type::findOrFail($request->commentable_id);

        $comment = new Comment;
        $comment->commenter()->associate(auth()->user());
        $comment->commentable()->associate($model);
        $comment->comment = $request->message;
        $comment->save();

        return redirect()->to(url()->previous() . '#comment-' . $comment->id);
    }
public function store(Request $request)
    {
        $this->authorize('create-comment', Comment::class);
        
        $this->validate($request, [
            'commentable_type' => 'required|string',
            'commentable_id' => 'required|integer|min:1',
            'message' => 'required|string'
        ]);

        $model = $request->commentable_type::findOrFail($request->commentable_id);

        $comment = new Comment;
        $comment->commenter()->associate(auth()->user());
        $comment->commentable()->associate($model);
        $comment->comment = $request->message;
        $comment->save();

        return redirect()->to(url()->previous() . '#comment-' . $comment->id);
    }
public function store ( Request $ request ) { $ this -> authorize ( 'create-comment' , Comment :: class ) ; $ this -> validate ( $ request , [ 'commentable_type' => 'required|string' , 'commentable_id' => 'required|integer|min:1' , 'message' => 'required|string' ] ) ; $ model = $ request -> commentable_type :: findOrFail ( $ request -> commentable_id ) ; $ comment = new Comment ; $ comment -> commenter ( ) -> associate ( auth ( ) -> user ( ) ) ; $ comment -> commentable ( ) -> associate ( $ model ) ; $ comment -> comment = $ request -> message ; $ comment -> save ( ) ; return redirect ( ) -> to ( url ( ) -> previous ( ) . '#comment-' . $ comment -> id ) ; }
Creates a new comment for given model.
Creates a new comment for given model .