vetor / laravel-collect
laravel collect
1.1.2
2019-06-06 10:56 UTC
Requires
- php: ^7.0
- illuminate/database: ~5.5.0|~5.6.0|~5.7.0
- illuminate/support: ~5.5.0|~5.6.0|~5.7.0
Requires (Dev)
- mockery/mockery: ^1.0
- orchestra/database: ~3.5.0|~3.6.0|~3.7.0
- orchestra/testbench: ~3.5.0|~3.6.0|~3.7.0
- phpunit/phpunit: ^6.0|^7.0
This package is auto-updated.
Last update: 2025-01-06 23:25:33 UTC
README
laravel collect
安装
使用 composer 安装:
$ composer require vetor/laravel-collect
模型迁移
运行模型迁移命令:
$ php artisan migrate
使用方法
User Model
use Illuminate\Foundation\Auth\User as Authenticatable;
use Vetor\Laravel\Collect\Collector\Models\Traits\Collector;
use Vetor\Contracts\Collect\Collector\Models\Collector as CollectorContract;
class User extends Authenticatable implements CollectorContract
{
use Collector;
}
Article Model
use Vetor\Laravel\Collect\Collectable\Models\Traits\Collectable;
use Vetor\Contracts\Collect\Collectable\Models\Collectable as CollectableContract;
class Article extends Model implements CollectableContract
{
use Collectable;
}
Available Methods
User
// 收藏
$user->collect($article);
// 是否收藏
$user->isCollectThis($article);
// 取消收藏
$user->cancelCollect($article);
// 用户的所有收藏记录
$user->collections;
// 用户收藏的文章记录
$user->collectionsWhereCollectable(Article::class);
Article
// 收藏
$article->collect();
// 是否收藏
$article->isCollection();
// 取消收藏
$article->cancelCollect();
注:默认为当前用户,可以把用户实例作为参数传入。
// 获取文章的收藏情况
$article->collections();
// 获取文章收藏数
$article->collections_count;
// 根据收藏数排序
Article::orderByCollectionsCount()->get();
注:升序 'asc';降序 'desc';默认为升序。
Collection
// 获取收藏表里所有文章
Collection::whereCollectable(Article::class)->get();