Skip to content
Advertisement

gor error message when deleteing comment in Laravel project

working with Laravel 6 project and I have following CommentController,

public function update(Request $request, $id)
    {
        $comment = Comment::find($id);

        $this->validate($request, array('comment' => 'required'));

        $comment->comment = $request->comment;
        $comment->save();

        Session::flash('success','Comment Created');

        return redirect()->route('posts.show', $comment->post->id);
    }

    public function delete($id)
    {
        $comment = Comment::find($id);
        return view('comments.delete')->withComment($comment);
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return IlluminateHttpResponse
     */
    public function destroy($id)
    {
        $comment = Comment::find($id);
        $post_id = $comment->post->id;
        $comment->delete();

        Session::flash('success','Deleted Comment');

        return redirect()->route('posts.show', $post_id);
    }

and My routes are as following

Route::delete('comments/{id}', ['uses' => 'CommentsController@destroy', 'as' => 'comments.destroy']);

Route::get('comments/{id}/delete', ['uses' => 'CommentsController@delete', 'as' => 'comments.delete']);

but when I try to delete comment got following validation error message The comment field is required.

how could I fix this problem here?

edit.blade.php

@section('content')
<div class="col-sm-6">
                <form action="{{ route('comments.destroy',  $comment->id) }}" method="post">
    @csrf
    {{ method_field('PUT') }}
    <button type="submit" class="btn btn-danger btn-block">Delete</button>
                </form>
            </div>
        </div>
    </div>

@endsection

Advertisement

Answer

Because HTML forms can’t send PUT, PATCH and DELETE requests, we must “spoof” the request and include an input field that tells Laravel which type it is sending. You can read about that here.

Technically, this means inserting the following for your delete form.

<input type="hidden" name="_method" value="delete">

Laravel comes with different helpers that help you achieve this. In your update form, you have already done this by adding the method_field('PUT');. Here, you are instructing Laravel that this is a PUT request, and we need to do this too in your delete form.

The {{ method_field('PUT') }} simply needs to change to {{ method_field('DELETE') }} or you can use the built-in @method('delete')

Like this

@section('content')
<div class="col-sm-6">
                <form action="{{ route('comments.destroy',  $comment->id) }}" method="post">
    @csrf
    @method('delete')
    <button type="submit" class="btn btn-danger btn-block">Delete</button>
                </form>
            </div>
        </div>
    </div>

@endsection

You can inspect your form HTML in the browser and notice the hidden input field added automatically.

User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement