E
E
Emil Rakhmatullin2020-06-10 15:10:40
Laravel
Emil Rakhmatullin, 2020-06-10 15:10:40

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>

But is this the right decision? This table can get incredibly large, and I'm worried about that. I don't know how the site will behave.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jazzus, 2020-06-10
@Emchik

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');

I
Ilya, 2020-06-10
@New_Horizons

Incredibly large is how many?) several million records? It's about nothing. It is only necessary to arrange the indexes correctly.
I just don't see any dislikes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question