This package gives Eloquent models the ability to manage their friendships.You can easily design a Facebook like Friend System.
First, install the package through Composer.
composer require hootlex/laravel-friendships
If you are using Laravel < 5.5, you need to add Hootlex\Friendships\FriendshipsServiceProvider to your config/app.php
providers array:
Hootlex\Friendships\FriendshipsServiceProvider::class,
Publish config and migrations
php artisan vendor:publish --provider="Hootlex\Friendships\FriendshipsServiceProvider"
Configure the published config in
config\friendships.php
Finally, migrate the database
php artisan migrate
use Hootlex\Friendships\Traits\Friendable;
class User extends Model
{
use Friendable;
...
}
Check the Test file to see the package in action
$user->befriend($recipient);
$user->acceptFriendRequest($sender);
$user->denyFriendRequest($sender);
$user->unfriend($friend);
$user->blockFriend($friend);
$user->unblockFriend($friend);
$user->isFriendWith($friend);
$user->hasFriendRequestFrom($sender);
$user->hasSentFriendRequestTo($recipient);
$user->hasBlocked($friend);
$user->isBlockedBy($friend);
$user->getFriendship($friend);
$user->getAllFriendships();
$user->getPendingFriendships();
$user->getAcceptedFriendships();
$user->getDeniedFriendships();
$user->getBlockedFriendships();
$user->getFriendRequests();
$user->getFriendsCount();
$user->getPendingsCount();
$user->getMutualFriendsCount($otherUser);
To get a collection of friend models (ex. User) use the following methods:
$user->getFriends();
$user->getFriends($perPage = 20);
$user->getFriendsOfFriends($perPage = 20);
$user->getFriends($perPage = 20, $group_name);
$user->getMutualFriends($otherUser, $perPage = 20);
The friend groups are defined in the config/friendships.php
file.The package comes with a few default groups.To modify them, or add your own, you need to specify a slug
and a key
.
// config/friendships.php
...
'groups' => [
'acquaintances' => 0,
'close_friends' => 1,
'family' => 2
]
Since you've configured friend groups, you can group/ungroup friends using the following methods.
$user->groupFriend($friend, $group_name);
$user->ungroupFriend($friend, 'family');
$user->ungroupFriend($friend);
$user->getFriendsCount($group_name);
friendships
by group you can pass a group slug.$user->getAllFriendships($group_name);
$user->getAcceptedFriendships($group_name);
$user->getPendingFriendships($group_name);
...
This is the list of the events fired by default for each action
Event name | Fired |
---|---|
friendships.sent | When a friend request is sent |
friendships.accepted | When a friend request is accepted |
friendships.denied | When a friend request is denied |
friendships.blocked | When a friend is blocked |
friendships.unblocked | When a friend is unblocked |
friendships.cancelled | When a friendship is cancelled |
See the CONTRIBUTING guide.
Laravel 是一套简洁、优雅的PHP Web开发框架(PHP Web Framework)。它可以让你从面条一样杂乱的代码中解脱出来;它可以帮你构建一个完美的网络APP,而且每行代码都可以简洁、富于表达力。 功能特点 1、语法更富有表现力 你知道下面这行代码里 “true” 代表什么意思么? $uri = Uri::create(‘some/uri’, array(), array(), tr
我需要空间/Laravel权限的帮助。当我试图分配它给我错误哎呀,看起来像出了问题。 错误 Connection.php第761行中的QueryExcema:SQLSTATE[23000]:完整性约束冲突:1048列role_id不能为空(SQL:插入到(,)值(9,))
Laravel 作为现在最流行的 PHP 框架,其中的知识较多,所以单独拿出来写一篇。 简述 Laravel 的生命周期 Laravel 采用了单一入口模式,应用的所有请求入口都是 public/index.php 文件。 注册类文件自动加载器 : Laravel通过 composer 进行依赖管理,无需开发者手动导入各种类文件,而由自动加载器自行导入。 创建服务容器:从 bootstrap/ap
简介 Laravel Scout 为 Eloquent 模型 全文搜索提供了简单的,基于驱动的解决方案。通过使用模型观察者,Scout 会自动同步 Eloquent 记录的搜索索引。 目前,Scout 自带一个 Algolia 驱动;不过,编写自定义驱动很简单, 你可以轻松的通过自己的搜索实现来扩展 Scout。 安装 首先,通过 Composer 包管理器来安装 Scout: composer
简介 Laravel 致力于让整个 PHP 开发体验变得愉快, 包括你的本地开发环境。 Vagrant 提供了一种简单,优雅的方式来管理和配置虚拟机。 Laravel Homestead 是一个官方预封装的 Vagrant box,它为你提供了一个完美的开发环境,而无需在本地机器安装 PHP 、Web 服务器和其他服务器软件。不用担心会搞乱你的操作系统!Vagrant boxes 是一次性的。如果
WebStack-Laravel 一个开源的网址导航网站项目,具备完整的前后台,您可以拿来制作自己的网址导航。 部署 克隆代码: git clone https://github.com/hui-ho/WebStack-Laravel.git 安装依赖: composer installphp artisan key:generate 编辑配置: cp .env.example .env ...D