Laravel Relationship Events
Laravel Relationship Events is a package by Viacheslav Ostrovskiy that adds extra model relationship events. This package comes with the following traits that are used to register listeners on a model’s boot() method:
- HasOneEvents
- HasBelongsToEvents
- HasManyEvents
- HasBelongsToManyEvents
- HasMorphOneEvents
- HasMorphToEvents
- HasMorphManyEvents
- HasMorphToManyEvents
- HasMorphedByManyEvents
And from the above traits, here’s an example of a few events on a Country model that has many Users using the HasManyEventstrait:
namespace App\Models;
use App\User;
use Chelout\RelationshipEvents\Concerns\HasManyEvents;
use Illuminate\Database\Eloquent\Model;
class Country extends Model
{
use HasManyEvents;
protected $fillable = [
'name',
];
public function users()
{
return $this->hasMany(User::class);
}
public static function boot()
{
parent::boot();
static::hasManySaving(function ($parent, $related) {
Log::info("Saving user's country {$parent->name}.");
});
static::hasManySaved(function ($parent, $related) {
Log::info("User's country is now set to {$parent->name}.");
});
}
}
And the inverse of the relationship with this package might look like the following:
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Chelout\RelationshipEvents\Concerns\HasBelongsToEvents;
class User extends Model
{
use HasBelongsToEvents;
/**
* Get the country associated with the user.
*/
public function country()
{
return $this->belongsTo(Country::class);
}
protected static function boot()
{
parent::boot();
static::belongsToAssociating(function ($relation, $related, $parent) {
Log::info("Associating country {$parent->name} with user.");
});
static::belongsToAssociated(function ($relation, $related, $parent) {
Log::info("User has been assosiated with country {$parent->name}.");
});
}
}
Using an overloaded associate() method, you can fire two events belongsToAssociating and belongsToAssociated:
$country = App\Models\Country::first();
$user = factory(User::class)->create([
'name' => 'John Smith',
]);
// Assosiate user with country
// This will fire belongsToAssociating and belongsToAssociated events
$user->country()->associate($country);
Learn More
The package has documentation for each trait and association type. Check out the package on GitHub at chelout/laravel-relationship-events.
Filed in: Laravel Packages / Eloquent
Laravel Relationship Events的更多相关文章
- Laravel 的 Events(事件) 及 Observers(观察者)
你是否听说过单一职责原则(single responsibility principle)?我希望是的.它是程序设计的基本原则之一,它基本上的意思就是,一个类有且只有一个职责.换句话说,一个类必须且只 ...
- Laravel 的 API 认证系统 Passport 三部曲(二、passport的具体使用)
GQ1994 关注 2018.04.20 09:31 字数 1152 阅读 1316评论 0喜欢 1 参考链接 Laravel 的 API 认证系统 Passport 三部曲(一.passport安装 ...
- JavaScript Application Architecture On The Road To 2015
JavaScript Application Architecture On The Road To 2015 I once told someone I was an architect. It’s ...
- Laravel Many to Many Polymorphic Relationship
Many to many Polymorphic relationship is also a little bit complicated to understand. For example, i ...
- laravel model relationship
laravel支持多种模型之间的relation,对应着模型间的one2one, one2many,many2many,hasManyThrough,Polymorphic, many2many po ...
- laravel swoole Call to undefined method Illuminate\Events\Dispatcher::fire()
报错: Call to undefined method Illuminate\Events\Dispatcher::fire() Whoops\Run::handleError("Unca ...
- laravel 开发辅助工具
laravel 开发辅助工具 配置 添加服务提供商 将下面这行添加至 config/app.php 文件 providers 数组中: 'providers' => [ ... App\Plug ...
- Laravel 5.3 登录注册底层实现详解
每个控制器都使用 trait 来引入它们需要的方法 */ 用于处理用户登录认证 用于处理新用户注册 包含重置密码逻辑 用于处理重置密码邮件链接 认证需要的视图 包含了应用的基础布局文件 ...
- [php]laravel框架容器管理的一些要点
本文面向php语言的laravel框架的用户,介绍一些laravel框架里面容器管理方面的使用要点.文章很长,但是内容应该很有用,希望有需要的朋友能看到.php经验有限,不到位的地方,欢迎帮忙指正. ...
随机推荐
- js性能提高篇
,最后统一将DocumentFragment添加到页面. 该做法可以减少页面渲染dom元素的次数.经IE和Fx下测试,在append1000个元素时,效率能提高10%-30%
- Ubuntu下Git从搭建到使用详解
Ubuntu下Git从搭建到使用详解 一.git的搭建 (1).sudo apt-get update (2).sudo apt-get -y install git 符:安装最新版本方法: add- ...
- 1.2、CDH 搭建Hadoop在安装之前(CDH基于包的安装所需的权限)
CDH基于包的安装所需的权限 以下部分描述了使用Cloudera Manager进行基于软件包的CDH安装的用户权限要求.这些要求是安装和管理包和服务的标准UNIX系统要求. 所需特权 sudo由Cl ...
- metasploit framework(四):生成payload
RHOST是限制来源IP,这里不用修改 generate生成payload 假设'\x00'是坏字符,生成payload时就会对'\x00'进行编码,排除坏字符. -b 去掉坏字符 -t 指定生成的格 ...
- day22 面向对象基础
1.什么是面向过程 在介绍面向对象之前,要先明确面向过程 在这之前我们所写的任何代码都是面向过程的 什么是面向过程? 是一种编程思想 面对 朝向 在编写代码时,要时刻想着过程这两个字 过程指的是什么? ...
- Pandas汇总和处理缺失数据
汇总的函数 方法 说明 count 非NA的值数量 describe 针对Series和DataFrame列计算汇总统计 min.max 计算最小值和最大值 argmin.argmax 计算能够获取到 ...
- 每月IT摘录201901
技术 1.Jsessionid只是tomcat的对sessionid的叫法,其实就是sessionid:在其它的容器也许就不叫jsessionid了. 2.在 InnoDB 中,索引使用的数据结构是 ...
- pandas中series和dataframe之间的区别
series结构有索引,和列名组成,如果没有,那么程序会自动赋名为None series的索引名具有唯一性,索引可以数字和字符,系统会自动将他们转化为一个类型object. dataframe由索引和 ...
- 不包含数据和字母的Webshell
在做php20中的challenge5时学习到了php中可代替数字和字母的符号,悲剧的是这道题没做出来,目前先整理知识点吧555555555555 情况是:很多时候在敏感地方WAF会阻 ...
- zkClient的使用
ZKClient是由DataMeer的工程师StefanGroschupf和Peter Voss 一起开发的,在源生API接口基础上进行了封装,简化了ZK的复杂性. 1. 创建客户端方法:ZKClie ...