flarone/laravel-favorable

Laravel 赞同包,用于赞同、反对和收藏您的模型

0.0.7 2023-03-30 09:49 UTC

This package is auto-updated.

Last update: 2024-09-30 02:00:01 UTC


README

Laravel 赞同包,用于赞同、反对和收藏您的模型。

Composer 安装

composer require flarone/laravel-favorable

然后运行迁移

php artisan migrate

设置您的模型

class Article extends \Illuminate\Database\Eloquent\Model {
	use \Flarone\Favoriteable\Favoriteable;
}

示例用法

$model->favorite(); // favorite the model for current user
$model->favorite($myUserId); // pass in your own user id
$model->favorite(0); // just add favorites to the count, and don't track by user

$model->defavorite(); // remove favorite from the model
$model->defavorite($myUserId); // pass in your own user id
$model->defavorite(0); // remove favorites from the count -- does not check for user

$model->favoriteCount; // get count of favorites

$model->favorites; // Iterable Illuminate\Database\Eloquent\Collection of existing favorites

$model->favorited(); // check if currently logged in user favorited the model
$model->favorited($myUserId);

Model::whereFavoritedBy($myUserId) // find only models where user favorited them
	->with('favoriteCounter') // highly suggested to allow eager load
	->get();

扩展收藏模型

如果需要,您可以扩展收藏模型。为此,创建自己的收藏模型,并让它扩展以下收藏基础模型

\Flarone\Favoriteable\Models\Favorite::class

接下来,使用以下命令发布配置文件

php artisan vendor:publish --provider="Flarone\Favoriteable\FavoriteableServiceProvider" --tag="config"

赞同的配置文件将被复制到您的配置目录中。在此文件中,将 favorite_model 调整为您自己创建的模型。

致谢