Yes, that's because the gates in AuthServiceProvider are using User::hasLike :
public function hasLiked(Likeable $likeable): bool { if (! $likeable->exists) { return false; } return $likeable->likes() ->whereHas('user', fn($q) => $q->whereId($this->id)) ->exists(); }
You may change it to use a cache, like this (actual working solution) :
public function hasLiked(Likeable $likeable): bool { if (! $likeable->exists) { return false; } return $this->likes->contains( fn($like) => $like->likeable_type == get_class($likeable) && $like->likeable_id == $likeable->id ); }
It should minimize the number of queries. It's ugly but it works!
Result before with 15 queries (one per @can('like')) and after only 6!
@can('like')
Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment's permalink.
Hide child comments as well
Confirm
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Yes, that's because the gates in AuthServiceProvider are using User::hasLike :
You may change it to use a cache, like this (actual working solution) :
It should minimize the number of queries. It's ugly but it works!
Result before with 15 queries (one per
@can('like')
) and after only 6!