PHP code example of trendsoft / laravel-bookmark

1. Go to this page and download the library: Download trendsoft/laravel-bookmark library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

trendsoft / laravel-bookmark example snippets


use Illuminate\Database\Eloquent\Model;
use Trendsoft\LaravelBookmark\Traits\Bookmarker;

class User extends Model
{
    use Bookmarker;
}

use Illuminate\Database\Eloquent\Model;
use Trendsoft\LaravelBookmark\Traits\Bookmarkable;

class Post extends Model
{
    use Bookmarkable;
}

$user = User::find(1);
$post = Post::find(1);

$user->bookmark($post);
$user->unBookmark($post);
$user->toggleBookmark($post);

$user->hasBookmarked($post);
$post->isBookmarkedBy($user);

$bookmarks = $user->bookmarks()->with('bookmarkable')->paginate(20);

foreach($bookmarks as $bookmark){
    $bookmark->bookmarkable; // App\Post instance
}

foreach($post->bookmarkers as $user){
    echo $user->name;
}

$bookmarkers = $post->bookmarkers()->paginate(20);
foreach($bookmarkers as $user){
    echo $user->name;
}

//all
$user->bookmarks()->count();

//with type
$user->bookmarks()->withType(Post::class)->count();

// bookmarkers count
$post->bookmarkers()->count();

$users = User::withCount('bookmarks')->get();

foreach($users as $user){
    echo $user->bookmarks_count;
}

// Bookmarker
$users = App\User::with('bookmarks')->get();

foreach($users as $user) {
    $user->hasBookmarked($post);
}

// Bookmarkable
$posts = App\Post::with('bookmarks')->get();
// or
$posts = App\Post::with('bookmarkers')->get();

foreach($posts as $post){
    $post->isBookmarkedBy($user);
}