namest / likeable
v0.1.2
2015-03-01 20:50 UTC
Requires
- illuminate/database: ~5.0
- illuminate/support: ~5.0
Requires (Dev)
- mockery/mockery: ~0.9
- phpunit/phpunit: ~4.5
This package is not auto-updated.
Last update: 2024-09-28 16:46:35 UTC
README
提供一种优雅的方式来与你的优雅模型之间的“喜欢”功能进行交互。
注意:此包仅支持Laravel 5
安装
步骤 1:安装包
composer require namest/likeable
步骤 2:在你的 config/app.php
文件中注册服务提供者
return [ ... 'providers' => [ ... 'Namest\Likeable\LikeableServiceProvider', ], ... ];
步骤 3:发布包资源,包括:配置、迁移。打开你的终端并输入
php artisan vendor:publish --provider="Namest\Likeable\LikeableServiceProvider"
步骤 4:迁移已发布的迁移
php artisan migrate
步骤 5:使用一些特质来创建酷炫的功能
class User extends Model { use \Namest\Likeable\LikerTrait; // ... } class Post extends Model { use \Namest\Likeable\LikeableTrait; // ... }
步骤 6:阅读下面的API并开始愉快地使用吧
API
$user = \App\User::find(1); $post = \App\Post::find(2); $like = $user->like($post); // Return Namest\Likeable\Like instance $result = $user->unlike($post); // Return true when success and false on otherwise
$user = $like->liker; // Return model that like another model $post = $like->likeable; // Return model that was liked by another model
$posts = $user->likes; // Return likeable collection that liker was liked $users = $post->likers; // Return liker collection who like that post
$users = User::wasLike($post)->...->get(); // Return liker collection who like that post $posts = Post::likedBy($user)->...->get(); // Return post collection which was liked by the user
事件
namest.likeable.liking
触发时间:在 $liker
喜欢一个可喜欢对象之前
有效载荷
$liker
:执行此操作的谁$likeable
:将要被喜欢的对象
用法
\Event::listen('namest.likeable.liking', function ($liker, $likeable) { // Do something });
namest.likeable.liked
触发时间:在 $liker
喜欢了一个可喜欢对象之后
有效载荷
$liker
:执行此操作的谁$likeable
:被喜欢的对象$like
:喜欢实例
用法
\Event::listen('namest.likeable.liked', function ($liker, $likeable, $like) { // Do something });
namest.likeable.unliking
触发时间:在 $liker
取消对一个可喜欢对象的喜欢之前
有效载荷
$liker
:执行此操作的谁$likeable
:将要取消喜欢的对象
用法
\Event::listen('namest.likeable.unliking', function ($liker, $likeable) { // Do something });
namest.likeable.unliked
触发时间:在 $liker
取消对一个可喜欢对象的喜欢之后
有效载荷
$liker
:执行此操作的谁$likeable
:被取消喜欢的对象
用法
\Event::listen('namest.likeable.unliked', function ($liker, $likeable) { // Do something });