DEV Community

Discussion on: How to implement a simple like system with Laravel

Collapse
 
bdelespierre profile image
Benjamin Delespierre • Edited

assuming you've done everything in the tutorial right you should have a route "/like"

You can check it with php artisan route:list --name like

The button should look like this:

@if(!auth()->user()->hasLiked($post))
    <form action="/like" method="post">
        @csrf
        <input type="hidden" name="likeable" value="{{ get_class($post) }}">
        <input type="hidden" name="id" value="{{ $post->id }}">
        <button type="submit" class="btn btn-primary">
            Like
        </button>
    </form>
@else
    <button class="btn btn-secondary" disabled>
        {{ $post->likes()->count() }} likes
    </button>
@endif
Enter fullscreen mode Exit fullscreen mode

I believe you can figure out how to make the unlike button by yourself with the resources provided in the tutorial ;-)

Collapse
 
md3108 profile image
MD3108

Could u show how unlike works because I'm just managing to like but not dislike... #alsoBeginner

Thread Thread
 
md3108 profile image
MD3108

I get this error when I try my dislikeSQLSTATE[22007]: Invalid datetime format: 1292 Truncated incorrect INTEGER value: 'like' (SQL: delete fromnoteswhereid= like

Thread Thread
 
bdelespierre profile image
Benjamin Delespierre

Sure, I think I'll update the article to add the views, stay tuned!

Thread Thread
 
bdelespierre profile image
Benjamin Delespierre

Aaaaand done. check it out!

Thread Thread
 
md3108 profile image
MD3108

ohh nice, couldn't have hoped for better. I'll leave a comment on how it went once I get to it again!

Thread Thread
 
bdelespierre profile image
Benjamin Delespierre

most welcome my friend. You can also check this out, it's a working implementation of the like system github.com/bdelespierre/laravel-li...

Thread Thread
 
md3108 profile image
MD3108

I managed to get as far as last time same error presists...
SQLSTATE[22007]: Invalid datetime format: 1292 Truncated incorrect INTEGER value: 'like' (SQL: delete fromnoteswhereid= like)`

however I think in 'app/Models/Concerns/Likes.php' the 'use App\Like' should be 'use App\Models\Like'. For the rest I got a flaw less exp. just don't know how to solve that issue there.