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的更多相关文章

  1. Laravel 的 Events(事件) 及 Observers(观察者)

    你是否听说过单一职责原则(single responsibility principle)?我希望是的.它是程序设计的基本原则之一,它基本上的意思就是,一个类有且只有一个职责.换句话说,一个类必须且只 ...

  2. Laravel 的 API 认证系统 Passport 三部曲(二、passport的具体使用)

    GQ1994 关注 2018.04.20 09:31 字数 1152 阅读 1316评论 0喜欢 1 参考链接 Laravel 的 API 认证系统 Passport 三部曲(一.passport安装 ...

  3. 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 ...

  4. Laravel Many to Many Polymorphic Relationship

    Many to many Polymorphic relationship is also a little bit complicated to understand. For example, i ...

  5. laravel model relationship

    laravel支持多种模型之间的relation,对应着模型间的one2one, one2many,many2many,hasManyThrough,Polymorphic, many2many po ...

  6. laravel swoole Call to undefined method Illuminate\Events\Dispatcher::fire()

    报错: Call to undefined method Illuminate\Events\Dispatcher::fire() Whoops\Run::handleError("Unca ...

  7. laravel 开发辅助工具

    laravel 开发辅助工具 配置 添加服务提供商 将下面这行添加至 config/app.php 文件 providers 数组中: 'providers' => [ ... App\Plug ...

  8. Laravel 5.3 登录注册底层实现详解

          每个控制器都使用 trait 来引入它们需要的方法 */ 用于处理用户登录认证 用于处理新用户注册 包含重置密码逻辑 用于处理重置密码邮件链接   认证需要的视图 包含了应用的基础布局文件 ...

  9. [php]laravel框架容器管理的一些要点

    本文面向php语言的laravel框架的用户,介绍一些laravel框架里面容器管理方面的使用要点.文章很长,但是内容应该很有用,希望有需要的朋友能看到.php经验有限,不到位的地方,欢迎帮忙指正. ...

随机推荐

  1. opencv批量读取图片

    #include<opencv2/opencv.hpp>using namespace cv;using namespace std;int main(){    int num=4;// ...

  2. db2start提示SQL5043,关闭连接终端tty

    db2 V10.3启动的时候提示: 问题1: db2start执行后提示: SQL1072C  The database manager resources are in an inconsisten ...

  3. Python 多继承与MRO-C3算法

    继承关系图:树结构 广度优先遍历:先找A,再找B.C,最后找D.E.(顺序:A.B.C) 深度优先遍历:先找A,再找B,接着找D.E(把B里面找完):然后找C.(顺序:A.B.D.E.C) MRO-C ...

  4. 算法之LOWB三人组之插入排序

    插入排序 思想:类似于抽扑克牌,共有8张扑克牌,手里默认有一张,桌面上有7张,我们每次从桌面上抽一张和手里的牌进行比较,如果比手里的牌大,则直接放到手里的牌的后面,如果比手里的牌小,则放到手里的牌的前 ...

  5. HTTP协议之请求

    HTTP请求 组成 一个http请求通常由三个部分组成: 请求行(request line) 首部(header) 主体(body) 格式如下所示 <request-line><CR ...

  6. 在window下搭建Vue.Js开发环境(转)

    nodejs官网http://nodejs.cn/下载安装包,无特殊要求可本地傻瓜式安装,这里选择2017-5-2发布的 v6.10.3 cmd命令行: node -v //显示node版本 v6.1 ...

  7. vue table中使用多选的问题(翻页后如何保存已选项),联动echarts图表实现流量监控

    流量监控项目需求: 根据表格数据,添加多选功能,默认全选,根据已选项更新图表视图 1.表格需要多选 2.要联动图表,所以关键是要利用表格多选的触发回调函数 vue table中使用多选: 很简单,只需 ...

  8. Python delattr() 函数

    Python delattr() 函数  Python 内置函数 描述 delattr 函数用于删除属性. delattr(x, 'foobar') 相等于 del x.foobar. 语法 dela ...

  9. .net 报错access to the path c:\tempimagefiles\msc_cntr_0.txt is denied

    报错信息: 解决方法: 在 Web.Config 的 <System.Web> 里加 <identity impersonate="true"/> 节点即可 ...

  10. latex安装及基本使用

    安装 需要两个文件,texlive2018.iso和texstudio-2.12.14-win-qt5.exe. 首先安装texlive2018.iso,管理员权限运行其中的install-tl-ad ...