gridprinciples/friendly

此包最新版本(0.1.0)没有提供许可证信息。

Laravel 上的用户好友系统。

0.1.0 2015-08-25 13:44 UTC

This package is not auto-updated.

Last update: 2024-09-28 18:21:19 UTC


README

这是一个为增强 Eloquent 用户之间的连接(包括用户双方的批准和附加数据)而设计的 Laravel 5.1 包。

Friendly 包含了许多创建自己的好友系统的快捷方式,但开发者可以自由地以他们所需的方式实现细节。

安装

  1. 在项目目录中运行 composer require gridprinciples/friendly

  2. 将以下内容添加到 config/app.php 文件中的 providers 数组。

    GridPrinciples\Friendly\Providers\FriendlyServiceProvider::class,
  3. 发布迁移和配置文件

    php artisan vendor:publish --provider="GridPrinciples\Friendly\Providers\FriendlyServiceProvider"
    
  4. 运行迁移

    php artisan migrate
    

    这将向您的数据库添加一个 friends 表来跟踪模型之间的关系。

用法

Friendly 特性添加到您的 User 模型
use Friendly;

现在您可以将用户相互关联

// Using the "requesting" user model...
$dad = User::where('name', 'darth')->first();

// ...and the one receiving the friend request...
$kid = User::where('name', 'luke')->first();

// may include "pivot" data.
$dad->befriend($kid, [
    'name'       => 'Father',
    'other_name' => 'Son',
    'start'      => '1980-05-21',
]);

// ...and later, the secondary user approves the request:
$kid->approve($dad);

// At this point, either user is associated with the other via their `friends` attribute (a Collection):
$relatives = $kid->friends->toArray();

// Either user may sever the relationship, resulting in a type of block:
$kid->block($dad);

连接数据

包含的实现表还包括以下关于每个好友请求的额外信息

  • name:描述请求用户与次要用户的关系。
  • other_name:描述次要用户与请求用户的关系。
  • start:关系开始或将开始的时间。
  • end:关系的结束时间。

重新加载连接

当您访问用户模型的 "friends" 属性时,连接将被加载和缓存。如果您更改了会影响已加载用户好友关系的不同关系,可能需要重新加载它们。

$user->resetFriends();