Looking to hire Laravel developers? Try LaraJobs

laravel-bookmarks maintained by pharaonic

Description
Polymorphic bookmarks for Laravel Eloquent models, with JSON-object metadata and database-enforced uniqueness.
Last update
2026/11/13 16:55 (dev-develop)
License
Links
Downloads
2

Comments
comments powered by Disqus

Installation

composer require pharaonic/laravel-bookmarks
php artisan migrate

The service provider is registered automatically, and migrate creates the bookmarks table. Publish the migration only to change it (--tag=bookmarks-migrations), and the configuration only to rename the table, use your own Bookmark model, or turn off cleanup on delete:

php artisan vendor:publish --tag=bookmarks-config

Usage

Add CanBookmark to the model that bookmarks (it does not have to be a user) and HasBookmarks to the model that is bookmarked. A model can use both.

use Illuminate\Foundation\Auth\User as Authenticatable;
use Pharaonic\Laravel\Bookmarks\Concerns\CanBookmark;

class User extends Authenticatable
{
    use CanBookmark;
}
use Illuminate\Database\Eloquent\Model;
use Pharaonic\Laravel\Bookmarks\Concerns\HasBookmarks;

class Post extends Model
{
    use HasBookmarks;
}
$bookmark = $user->bookmark($post, ['note' => 'Read later', 'preferences' => ['notify' => true]]);

$bookmark->meta->note;                 // 'Read later'
$bookmark->meta->preferences->notify;  // true
$bookmark->meta->get('priority', 0);   // 0: the key is absent

$user->bookmark($post, ['priority' => 2]); // replaces the whole metadata: note is gone
$user->bookmark($post);                    // keeps {"priority":2}
$user->bookmark($post, []);                // empties it to {}

$user->bookmarkOf($post);      // the Bookmark, or null
$user->hasBookmarked($post);   // true
$user->unbookmark($post);      // true: this call removed it, false when there was none
$user->toggleBookmark($post);  // the new Bookmark, or null when it was removed

Post::whereBookmarkedBy($user)->latest()->paginate();
Post::withBookmarkedBy($user)->withCount('bookmarks')->get(); // each post has is_bookmarked
$user->givenBookmarks()->with('bookmarkable')->latest()->get();

Documentation

Full documentation: Laravel Bookmarks Documentation.

Support & Security

See SUPPORT.md and SECURITY.md. Contributions are welcome; see CONTRIBUTING.md.

License

MIT. See LICENSE.