Mahmoud Al-HabashOpen to new projects
  • WorkWork
  • ServicesServices
  • LabLab
  • WritingWriting
  • ContactContact
Amman--:--عربي
Writing/Livewire

Building a Bookmark Feature with Livewire v4

Save items for later with bookmarks.

1 February 20261 min readLivewireاقرأ بالعربية

On this page

  1. Component

Build a bookmark/save feature.

Component

php
@php
new class extends Livewire\Component {
    public int $postId;
    public bool $bookmarked = false;
    
    public function mount(int $postId): void
    {
        $this->postId = $postId;
        $this->bookmarked = auth()->user()->bookmarks()->where('post_id', $postId)->exists();
    }
    
    public function toggle(): void
    {
        if ($this->bookmarked) {
            auth()->user()->bookmarks()->detach($this->postId);
        } else {
            auth()->user()->bookmarks()->attach($this->postId);
        }
        $this->bookmarked = !$this->bookmarked;
    }
}
@endphp
All articles

Keep reading

  • Authorization in Livewire v4 Components1 February 20261 min read
  • Blaze Pre-Compiler: Massive Performance Gains in Livewire v41 February 20261 min read
  • Building a Color Picker with Livewire v41 February 20261 min read