Laravel:MethodNotAllowedHttpException错误尝试喜欢帖子

On my website users are allowed to create posts and like posts. I had a previous error relating to the like system before this and I managed to get that resolved. However with this error I can't figure out what it is. When a user clicks like they are presented with the following error:

Symfony \ Component \ HttpKernel \ Exception \ MethodNotAllowedHttpException

This is my post controller method for the like:

public function postLikePost($post_id){
    $loggedin_user = Auth::user()->id;
    $like_user = Like::where(['user_id' => $loggedin_user, 'post_id' => $post_id])->first();
    if(empty($like_user->user_id)){
        $user_id = Auth::user()->id;
        $post_id = $post_id;

        $like = new Like;

        $like->user_id = $user_id;
        $like->post_id = $post_id;
        $like->save();
        return  redirect()->route('events');

    }else{
        return  redirect()->route('events');
    }

}

This is my Like model:

class Like extends Model
{
    public function user(){
        return $this->belongsTo('App\User');
    }

    public function post(){
        return $this->belongsTo('App\Post');
    }
}

This is my likes migration:

    Schema::create('likes', function (Blueprint $table) {
        $table->increments('id');
        $table->integer('post_id');
        $table->integer('user_id');
        $table->timestamps();
    });

This is my route for the like:

Route::post('/like/{post_id}', 'PostController@postLikePost')->name('like');

This my view for the post:

<section class="row posts">
    @foreach($posts as $post)
        <div class="col-md-2 col-md-offset-3">
            <article class="post">
                <p>{{ $post->body }}</p>
                <div class="info">Posted by {{ $post->user->first_name }} {{ $post->user->last_name }} on {{ $post->created_at }}</div>
                <p>This post has {{ $post->likes()->count() }} likes </p> 
                <a href="{{ route('like', ['post_id' => $post->id]) }}" class="post-item">Like</a>|
            </article>
        </div>
    @endforeach
</section>

Either convert the like button to a form with POST request or submit the request via Ajax as a POST request.

Alternatively, update your route to a GET request (if you want to keep the current blade template as it is):

Route::get('/like/{post_id}', 'PostController@postLikePost')->name('like');

The MethodNotAllowed error is regularly the method used in the route, for example when you want to access to a method by GET being POST and around.

In this case the error is that you are using the method POST but you are redirecting to the route with a tag a which the uses the method GET, so you have to change the route from POST to GET like this:

Routes:

Route::post('/like/{post_id}', 'PostController@postLikePost')->name('like');

to

Route::get('/like/{post_id}', 'PostController@postLikePost')->name('like');

That is all that you need to change, regards!

In addition if you want to perform multiple methods on one route then you can also use below method specified by Laravel Framework.

Route::any('/like/{post_id}', 'PostController@postLikePost')->name('like');

and

Route::match(['GET','POST'],'/like/{post_id}', 'PostController@postLikePost')->name('like');