Preciso mudar os like e deslike (tipo boolean) com Laravel 6.0 e para isso estou utilizando JQuery e Ajax para realizar requisição em background no controller, mas de alguma forma não estou acessando o controller de Posts.
Não gera erro clicando no like do frontend, mas se eu for direto para http://127.0.0.1:8999/like Porque o GET?
Rotas
Route::post('/like', 'PostController@postLike')->name('post.like');
Route::get('artigos', 'PostController@index')->name('post.index');
Route::get('artigos/novo', 'PostController@create')->name('post.create');
Route::get('artigos/{post}', 'PostController@show')->name('post.show');
Route::post('artigos', 'PostController@store')->name('post.store');
app.js
$('.like').on('click', function(event) {
event.preventDefault();
postId = event.target.parentNode.parentNode.dataset['postid'];
var isLike = event.target.previousElementSibling == null;
$.ajax({
method: 'POST',
url: urlLike,
data: {isLike: isLike, postId: postId, _token: token}
}).done(function() {
event.target.innerText = isLike ? event.target.innerText == 'Like' ? 'You like this post' : 'Like' : event.target.innerText == 'Dislike' ? 'You dont like this post' : 'Dislike';
if (isLike) {
event.target.nextElementSibling.innerText = 'Dislike';
} else {
event.target.previousElementSibling.innerText = 'Like';
}
});
});
Método na PostController
public function postLike(Request $request)
{
$post_id = $request['postId'];
$is_like = $request['isLike'] === 'true';
$update = false;
$post = Post::find($post_id);
if (!$post) {
return null;
}
$user = Auth::user();
$like = $user->likes()->where('post_id', $post_id)->first();
if ($like) {
$already_like = $like->like;
$update = true;
if ($already_like == $is_like) {
$like->delete();
return null;
}
} else {
$like = new Like();
}
$like->like = $is_like;
$like->user_id = $user->id;
$like->post_id = $post->id;
if ($update) {
$like->update();
} else {
$like->save();
}
return null;
}
index.blade.php
@extends('admin.master.layout')
@section('content')
<main role="main" class="col-md-9 ml-sm-auto col-lg-10 pt-3 px-4">
<table class="table" id="formCad">
<div class="row featurette">
@foreach($posts as $post)
<div class="col-md-12" data-postid="{{ $post->id }}">
<h4 class="featurette-heading teste">{{$post->id}} | {{$post->title}}</h4>
<h3 class="lead text-muted">{{$post->content}}.</h3>
@foreach($post->categories as $cat)
<p class="badge badge-success">{{$cat->title}}</p>
@endforeach
<p class="blockquote-footer">Posted by {{ $post->user->name }} on
{{date('d/m/yy H:II', strtotime($post->user->created_at))}} |
Views: {{$post->view}}
</p>
<div class="interaction">
<a href="#" class="m-1 like">
{{ Auth::user()->likes()->where('post_id', $post->id)->first() ? Auth::user()->likes()->where('post_id', $post->id)->first()->like == 1 ? 'You like this post' : 'Like' : 'Like' }}
</a>
<a href="#" class="m-1 like">
{{ Auth::user()->likes()->where('post_id', $post->id)->first() ? Auth::user()->likes()->where('post_id', $post->id)->first()->like == 0 ? 'You dont like this post' : 'Dislike' : 'Dislike' }}
</a>
</div>
</div>
@endforeach
</div>
</table>
</main>
<script>
var token = '{{ Session::token() }}';
```var urlLike = '{{ route('post.like') }}';
</script>
@endsection