لايف واير

بناء زر إعجاب مع Livewire v4

February 01, 2026 1 دقيقة قراءة 10 مشاهدة

ابنِ زر إعجاب مع واجهة متفائلة.

المكون

@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
شارك هذه المقالة:

مقالات ذات صلة

التعليقات (0)

يرجى تسجيل الدخول لترك تعليق. تسجيل الدخول

لا توجد تعليقات بعد. كن أول من يعلق!