I have a created simple like system with Laravel framework
Here's my table schema look like:
Schema::create('likes', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('by_id');
$table->unsignedBigInteger('post_id');
$table->timestamps();
});
My Eloquent relationship is simple:
Auth::user()->likes()->create(['post_id' => 'xxx']);
It's working as I want it to be
And In like page, I can show all posts that user xxx likes
@foreach ($user->likes as $post)
...
@endforeach
Pretty much simple
And now in home page, I want to show random posts
So I code this way:
$posts = Post::inRandomOrder()->get();
@foreach ($posts as post)
...
// In each of these random post, check if user already like the post or not
// If they do, show a button to delete the like
// Else show button to like the post
@endforeach
In each of those random post, I want to know if current login user already like the post or not
So how to achieve this?
Sorry for bad english
Thanks in advance