Answer the question
In order to leave comments, you need to log in
How to like on Laravel?
There are articles. You need to add the ability to like or dislike.
I am aware of this solution:
// Used by model App\Like
Schema::create('likes', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('likeable_id');
$table->string('likeable_type');
$table->unsignedInteger('user_id');
$table->timestamps();
});
// Post & Comment model
public function likes()
{
return $this->morphMany('App\Like', 'likeable');
}
// Then you can do something like
<p>
This post has {{ $posts->likes()->count() }} likes
</p>
Answer the question
In order to leave comments, you need to log in
As it becomes incredibly large and slow, then you can easily and simply separate the problematic entity. Those. solve the problem when it appears, not in advance. This is if it appears. In the meantime, you can do it as in the example, just remove likes in the trait to the same methods with likes (write / delete, etc.) so that the code is not duplicated, instead
$table->unsignedInteger('likeable_id');
$table->string('likeable_type');
write
AND count of likes can be written and incremented in the database so that no requests are made $table->morphs('likeable');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question