laravel-interaction / favorite
User favorite/unfavorite behaviour for Laravel.
3.3.0
2024-03-18 14:48 UTC
Requires
- php: ^8.0
- illuminate/database: ^8.13 || ^9.0 || ^10.0 || ^11.0
- illuminate/support: ^8.0 || ^9.0 || ^10.0 || ^11.0
- laravel-interaction/support: ^1.0 || ^2.0 || ^3.0
Requires (Dev)
- mockery/mockery: ~1.3.3 || ^1.4.2
- orchestra/testbench: ^6.0 || ^7.0 || ^8.0 || ^9.0
- phpunit/phpunit: ^9.3.3 || ^10.0
This package is auto-updated.
Last update: 2024-11-13 13:11:45 UTC
README
User favorite/unfavorite behaviour for Laravel.
Introduction
It let people express how they feel about the model(documentation/subject/topic).
Installation
Requirements
Instructions
Require Laravel Favorite using Composer.
composer require laravel-interaction/favorite
Publish configuration and migrations
php artisan vendor:publish --tag=favorite-config php artisan vendor:publish --tag=favorite-migrations
Run database migrations.
php artisan migrate
Usage
Setup Favoriter
use Illuminate\Database\Eloquent\Model; use LaravelInteraction\Favorite\Concerns\Favoriter; class User extends Model { use Favoriter; }
Setup Favoriteable
use Illuminate\Database\Eloquent\Model; use LaravelInteraction\Favorite\Concerns\Favoriteable; class Channel extends Model { use Favoriteable; }
Favoriter
use LaravelInteraction\Favorite\Tests\Models\Channel; /** @var \LaravelInteraction\Favorite\Tests\Models\User $user */ /** @var \LaravelInteraction\Favorite\Tests\Models\Channel $channel */ // Favorite to Favoriteable $user->favorite($channel); $user->unfavorite($channel); $user->toggleFavorite($channel); // Compare Favoriteable $user->hasFavorited($channel); $user->hasNotFavorited($channel); // Get favorited info $user->favoriterFavorites()->count(); // with type $user->favoriterFavorites()->withType(Channel::class)->count(); // get favorited channels Channel::query()->whereFavoritedBy($user)->get(); // get favorited channels doesnt favorited Channel::query()->whereNotFavoritedBy($user)->get();
Favoriteable
use LaravelInteraction\Favorite\Tests\Models\User; use LaravelInteraction\Favorite\Tests\Models\Channel; /** @var \LaravelInteraction\Favorite\Tests\Models\User $user */ /** @var \LaravelInteraction\Favorite\Tests\Models\Channel $channel */ // Compare Favoriter $channel->isFavoritedBy($user); $channel->isNotFavoritedBy($user); // Get favoriters info $channel->favoriters->each(function (User $user){ echo $user->getKey(); }); $channels = Channel::query()->withCount('favoriters')->get(); $channels->each(function (Channel $channel){ echo $channel->favoriters()->count(); // 1100 echo $channel->favoriters_count; // "1100" echo $channel->favoritersCount(); // 1100 echo $channel->favoritersCountForHumans(); // "1.1K" });
Events
License
Laravel Favorite is an open-sourced software licensed under the MIT license.