manzadey/laravel-like

允许 Laravel Eloquent 模型实现 'like' 功能

v1.1.0 2022-05-24 03:01 UTC

This package is auto-updated.

Last update: 2024-09-24 08:17:50 UTC


README

Latest Version on Packagist Total Downloads Software License

本包实现了laravel框架中的点赞/踩功能。

索引

安装

您可以通过composer安装此包

  1. 通过Composer安装包
composer require manzadey/laravel-like
  1. 通过命令行发布数据库
php artisan vendor:publish --provider="Manzadey\LaravelLike\LikeServiceProvider"
  1. 通过命令行迁移数据库
php artisan migrate

发布配置文件

发布配置文件是可选的

php artisan vendor:publish --provider="Manzadey\LaravelLike\LikeServiceProvider" --tag="config"

模型

您的用户模型应导入 Likeability 特性,LikeabilityContract 实现接口并使用它,该特性允许用户对模型进行点赞。(以下是一个示例)

use Manzadey\LaravelLike\Traits\Likeability;
use Manzadey\LaravelLike\Contracts\LikeabilityContract;

class User extends Authenticatable implements LikeabilityContract;
{
	use Likeability;
}

您的模型应导入 Likeable 特性,LikeableContract 实现接口并使用它,该特性包含您将用于允许模型可点赞的方法。在所有示例中,我将使用 Post 模型作为 'Likeable' 的模型,这只是一个示例。(以下是一个示例)

use Manzadey\LaravelLike\Traits\Likeable;
use Manzadey\LaravelLike\Contracts\LikeableContract;

class Post extends Model implements LikeableContract
{
    use Likeable;
}

就是这样 ... 您的模型现在 "可点赞"!现在用户可以喜欢具有可点赞特性的模型。

使用

用于与包一起工作的标准方法集

$article = \App\Models\Article::first();
$user = \App\Models\User::first();

$article->like($user); // Add the current model to like of the specified user
$article->dislike($user); // Add the current model to dislike of the specified user
$article->toggleLike($user); // The switch of the likes of the specified user
$article->removeLikeable($user); // Delete the current module from the likes of the specified user
$article->isLike($user); // Check the current model whether it is a likes of the specified user
$article->isDislike($user); // Check the current model whether it is a likes of the specified user

您也可以将这些方法用作 'Likeability' 模型。

检查模型是否具有特定用户的点赞/踩记录

$article = \App\Models\Article::first();
$user = \App\Models\User::first();

$article->isLike($user);
$article->isDislike($user);

您也可以将这些方法用作 'Likeability' 模型。

检查当前模型,是否标记了点赞/踩

$article = \App\Models\Article::first();

$article->isLikes();
$article->isDislikes();

返回一个已点赞/踩此模型的用户集合

$article = \App\Models\Article::first();
$user = \App\Models\User::first();

$article->likeBy();
$article->dislikeBy();

我们获得了一个标记了点赞/踩的模型集合

$article = \App\Models\Article::first();
$user = \App\Models\User::first();

$user->getLike();
$user->getDisike();

// With usage models
$user->getLike([\App\Models\Article::class]);
$user->getDisike([\App\Models\Article::class]);

测试

composer test

变更日志

请参阅 CHANGELOG 了解最近更改的详细信息。

贡献

请参阅 CONTRIBUTING 了解详细信息。

安全

如果您发现任何安全相关的问题,请通过电子邮件 andrey.manzadey@gmail.com 反馈,而不是使用问题跟踪器。

鸣谢

许可证

MIT 许可证(MIT)。请参阅 许可证文件 了解更多信息。

Laravel Package Boilerplate

此包是用 Laravel Package Boilerplate 生成的。