inpin / lara-like

为Laravel Eloquent模型添加可点赞特性

安装: 934

依赖: 0

建议者: 0

安全性: 0

星标: 5

关注者: 2

分支: 1

开放问题: 0

类型:

1.0.4 2018-06-05 11:19 UTC

This package is auto-updated.

Last update: 2024-09-29 04:31:22 UTC


README

重要提示:此产品是从基础包 laravel-likeable 分支和编辑而来。

Build Status StyleCI Maintainability Latest Stable Version Total Downloads Latest Unstable Version License

为Laravel Eloquent模型添加可点赞特性,以方便实现“点赞”、“收藏”或“记住”等您需要的功能。

Composer安装(适用于Laravel 5.5及以上版本)

composer require inpin/lara-like

安装并运行迁移

'providers' => [
    \Inpin\LaraLike\LaraLikeServiceProvider::class,
],
php artisan vendor:publish --provider="Inpin\LaraLike\LaraLikeServiceProvider" --tag=migrations
php artisan migrate

设置您的模型

class Book extends \Illuminate\Database\Eloquent\Model {
    use Inpin\LaraLike\Likeable;
}

示例用法

$book->like(); // like the book for current user
$book->like($user); // pass in your own user
$book->like(0); // just add likes to the count, and don't track by user
$book->like('api'); // like the book for current user with guard 'api'
$book->like(null, 'bookmark') // add book for current user to bookmarks
$book->like($user, 'bookmark') // pass user and type

$book->unlike(); // remove like from the book
$book->unlike($user); // pass in your own user id
$book->unlike(0); // remove likes from the count -- does not check for user
$book->unlike('api'); // remove like from book for current user with guard 'api'
$book->unlike(null, 'bookmark') // remove current book from current user bookmarks
$book->unlike($user, 'bookmark') // pass user and type

$book->likes; // Iterable Illuminate\Database\Eloquent\Collection of existing likes 
$book->likes()->where('type', 'bookmark')

$book->liked(); // check if currently logged in user liked the book
$book->liked($myUserId);

$book->likeCount($type); // determine number of likes for given $type (default type is 'like')

Article::whereLikedBy($myUserId) // find only books where user liked them
	->with('likeCounter') // highly suggested to allow eager load
	->get();

注意:默认类型为'like'。

致谢