PHP code example of manzadey / laravel-favorite

1. Go to this page and download the library: Download manzadey/laravel-favorite 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/ */

    

manzadey / laravel-favorite example snippets


declare(strict_types=1);

return [
    
    /*
     * The fully qualified class name of the favorite model.
     */
    'model' => \Manzadey\LaravelFavorite\Models\Favorite::class,
    
];

use Manzadey\LaravelFavorite\Traits\Favoriteability;
use Manzadey\LaravelFavorite\Contracts\FavoriteabilityContract;

class User extends Authenticatable implements FavoriteabilityContract;
{
	use Favoriteability;
}

use Manzadey\LaravelFavorite\Traits\Favoriteable;
use Manzadey\LaravelFavorite\Contracts\FavoriteableContract;

class Post extends Model implements FavoriteableContract
{
    use Favoriteable;
}

$user = User::first();
$post = Post::find(1);
$post->addFavorite($user); // user with that id added to favorites this post
$post->removeFavorite($user); // user with that id removed from favorites this post
$post->toggleFavorite($user); // user with that id toggles the favorite status from this post

$user = User::first();
$post = Post::first();
$user->addFavorite($post); // The user added to favorites this post
$user->removeFavorite($post); // The user removed from favorites this post
$user->toggleFavorite($post); // The user toggles the favorite status from this post

$user = Auth::user();
$user->getFavorite(Post::class, Article::class); // returns a collection with the Posts the User marked as favorite

$post = Post::find(1);
$post->favoriteBy(); // returns a collection with the Users that marked the post as favorite.

$post = Post::find(1);
$post->isFavorite(); // returns a boolean with true or false.
shell
php artisan vendor:publish --provider="Manzadey\LaravelFavorite\FavoriteServiceProvider" --tag="migrations"
shell
php artisan migrate
shell
php artisan vendor:publish --provider="Manzadey\LaravelFavorite\FavoriteServiceProvider" --tag="config"