ابنِ زر إعجاب مع واجهة متفائلة.
المكون
@php
new class extends Livewire\Component {
public Post $post;
public bool $liked = false;
public int $count = 0;
public function mount(Post $post): void
{
$this->post = $post;
$this->liked = $post->isLikedBy(auth()->user());
$this->count = $post->likes_count;
}
public function toggle(): void
{
if ($this->liked) {
$this->post->unlike(auth()->user());
$this->count--;
} else {
$this->post->like(auth()->user());
$this->count++;
}
$this->liked = !$this->liked;
}
}
@endphp