Search by

pharaonic / laravel-bookmarks

Pharaonic

Polymorphic bookmarks for Laravel Eloquent models, with JSON-object metadata and database-enforced uniqueness.

Package info

github.com/Pharaonic/laravel-bookmarks

Documentation

pkg:composer/pharaonic/laravel-bookmarks

Fund package maintenance!

MoamenEltouny

Buy Me A Coffee

Statistics

Installs: 2

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 1

13.0.0 2026-11-13 09:59 UTC

README

PHP Version : 8.3 | 8.4 | 8.5 Laravel Version : 13.x License Build
Source Packagist Version Packagist Downloads

Polymorphic bookmarks for Eloquent models, with JSON-object metadata.


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.