The Vhnh Bookmark package allows bookmarking eloquent models within your Laravel application.
Frist we'll add the Vhnh\Bookmark\Bookmarker trait to our user model. The trait allows retrieving bookmarks and cascades the Vhnh\Bookmark\Bookmarks if the user will be deleted.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Vhnh\Bookmark\Bookmarker;
class User extends Model
{
use Bookmarker;
// ...
}Next we'll add the Vhnh\Bookmark\Bookmarkable trait to any models that can be bookmarked. This trait cascades a Vhnh\Bookmark\Bookmark if a bookmarkable instance will be deleted and adds the functionality to create and remove bookmarks.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Vhnh\Bookmark\Bookmarkable;
class Post extends Model
{
use Bookmarkable;
// ...
}Creating and removing bookmarks is as simple as calling the a method on our bookmarkable models.
<?php
namespace App\Http\Controllers;
use App\Post;
class BookmarkPostsController
{
public function store(Post $post)
{
$post->bookmark();
// ...
}
public function destroy(Post $post)
{
$post->unmark();
// ...
}
}However, since it is more common to toggle a bookmark we can simplify our controller.
<?php
namespace App\Http\Controllers;
use App\Post;
class BookmarkPostsController
{
public function __invoke(Post $post)
{
if($post->isBookmarked()) {
return $post->bookmark();
}
return $post->unmark();
}
}To retrieve a users bookmarks you may call the Vhnh\Bookmark\Bookmarker::bookmarks() relationship that is provided by the Vhnh\Bookmark\Bookmarker trait. The Vhnh\Bookmark\Bookmark model will eager load the related bookmarkable instance.
$user->bookmarksWe can also be more expecit by fetching the the bookmarks:
$user->bookmarks()->without('bookmarkable')->paginate(15);
$user->bookmarks()->with('bookmarkable.title')->latest()->limit(5)->get();
// etc.The Vhnh Bookmark package is open-sourced software licensed under the MIT license.