DEV Community

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

Collapse
 
theovier profile image
Theo Harkenbusch

Hey, thanks for the tutorial! However, I think there is a slight mistake in the Likes.php class.

It should be
return $likeable->likes()->whereHas('user', function(Builder $query) {
return $query->whereId($this->id);
})->exists();

rather than
return $likeable->likes()->whereHas('user', function($query) {
return $query->whereId($this->id)->exists();
});

in Laravel 7.

Collapse
 
stleroux profile image
stleroux • Edited

For some reason I get an error message when I try to post my own comment

Collapse
 
bdelespierre profile image
Benjamin Delespierre

My mistake. I'll change it, thanks for pointing it out

Collapse
 
mateator profile image
mateator • Edited

Ty man, i have been stuck for weeks because of that, it wasnt working until you wrote to took out the ->exists();

same thing to delete, the result is:
$likeable->likes()->whereHas('user', function(Builder $query) {
$query->whereId($this->id);
})->delete();

Also, if someone implements this, dont forget to add this in Likes.php
use Illuminate\Database\Eloquent\Builder;

Collapse
 
bdelespierre profile image
Benjamin Delespierre

Yeah I removed the Builder typehint, it wasn't very helpful anyway.