1. Go to this page and download the library: Download modularavel/favoritable 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/ */
modularavel / favoritable example snippets
use Modularavel\Favoritable\Traits\HasModularavelFavoritable;
class Post extends Model
{
use HasModularavelFavoritable;
}
// Check if favorited by a user
$post->isFavoritedBy($user);
// Toggle favorite
$post->toggleFavorite($user);
// Add favorite
$post->addFavorite($user);
// Remove favorite
$post->removeFavorite($user);
// Get favorites count
$post->favoritesCount();
// Query scope for favorited items
Post::favoritedBy($user)->get();
class Post extends Model
{
use Favoritable;
public function getFavoritableTitle(): string
{
return $this->title;
}
public function getFavoritableDescription(): ?string
{
return $this->excerpt;
}
public function getFavoritableImage(): ?string
{
return $this->featured_image;
}
public function getFavoritableUrl(): string
{
return route('posts.show', $this);
}
}
return [
// The fully qualified class name of the user model
'user_model' => env('FAVORITABLE_USER_MODEL', 'App\\Models\\User'),
// The name of the belongTo relationship on the favoritable model to the user model
'owner_relationship_name' => env('FAVORITABLE_OWNER_RELATIONSHIP_NAME', 'user'),
// Button configuration
'button' => [
'default_variant' => 'default',
'default_size' => 'md',
'show_count' => true,
],
// List configuration
'list' => [
'per_page' => 12,
],
];
use Modularavel\Favoritable\Models\Favorite as BaseFavorite;
class Favorite extends BaseFavorite
{
// Your custom code
}