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>