alibayat / likeable
为Laravel的Eloquent模型实现点赞系统。
dev-master
2020-09-26 18:36 UTC
Requires
- php: >=5.5.0
- illuminate/database: >=5.0
- illuminate/support: >=5.0
This package is auto-updated.
Last update: 2024-09-27 04:02:26 UTC
README
本包适用于Laravel 5+,使实现Eloquent模型点赞/收藏系统变得简单。只需在模型中使用特性即可。
Composer安装(适用于Laravel 5+)
composer require alibayat/likeable
发布并运行迁移
php artisan vendor:publish --provider="AliBayat\LaravelLikeable\LikeableServiceProvider" --tag=migrations
php artisan migrate
如果你使用的是Laravel 5.5+版本,Likeable包将自动被Laravel发现。如果不是,请手动在config/app.php的providers数组中注册该包。
'providers' => [ ... \AliBayat\LaravelLikeable\LikeableServiceProvider::class, ],
设置模型 - 只需在模型中使用特性。
<?php namespace App; use Illuminate\Database\Eloquent\Model; use AliBayat\LaravelLikeable\Likeable; class Post extends Model { use Likeable; }
用法
$post->like(); // like the post for current user $post->like($userId); // pass in the user id $post->like(0); // just adding likes to the count, and don't track any user $post->unlike(); // remove like from the post $post->unlike($userId); // pass in the user id $post->unlike(0); // remove likes from the count -- does not check for user $post->likeCount; // get Total count of likes $post->likes; // Collection (Illuminate\Database\Eloquent\Collection) of existing likes $post->liked(); // check if currently logged in user liked the post $post->liked($userId); // pass in the user id Post::likedBy($userId) // find only posts where user liked them ->with('likeCounter') // with eager loading ->get();
致谢
- Ali Bayat - ali.bayat@live.com