Laravel Eloquent ORM - 删除行和所有子关系,事件删除
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34989701/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel Eloquent ORM - removing rows and all the child relationship, with event deleting
提问by Kamol Hakimbaev
I have three models that relate to each other one to many:
我有三个相互关联的模型:
Country
国家
class Country extends Model
{
protected $fillable=['name','sort'];
public $timestamps=false;
public function region(){
return $this->hasMany('App\Models\Region');
}
}
Region
地区
class Region extends Model
{
protected $fillable=['country_id','name','sort'];
public $timestamps=false;
public function country()
{
return $this->belongsTo('App\Models\Country');
}
public function city()
{
return $this->hasMany('App\Models\City');
}
}
City
城市
class City extends Model
{
protected $table='cities';
protected $fillable=['region_id','name','sort'];
public $timestamps=false;
public function region()
{
return $this->belongsTo('App\Models\Region');
}
}
When we remove the country automatically, remove all child item relationship, that is, removed and regions and city this country
当我们自动移除国家时,移除所有子项的关系,即移除和地区和城市这个国家
I am doing so:
我这样做:
Model Country
模范国家
public static function boot() {
parent::boot();
static::deleting(function($country) {
//remove related rows region and city
// need an alternative variation of this code
$country->region()->city()->delete();//not working
$country->region()->delete();
return true;
});
}
}
OR
或者
Model Region
模型区域
public static function boot() {
parent::boot();
// this event do not working, when delete a parent(country)
static::deleting(function($region) {
dd($region);
//remove related rows city
$region->city()->delete();
return true;
});
}
}
options with cascading deletes database, please do not offer
带有级联删除数据库的选项,请不要提供
UPDATE
更新
I found the answer
我找到了答案
use closure for query builder, to remove related models
对查询构建器使用闭包,删除相关模型
Model Country
模范国家
public static function boot() {
parent::boot();
static::deleting(function($country) {
//remove related rows region and city
$country->region->each(function($region) {
$region->city()->delete();
});
$country->region()->delete();//
return true;
});
}
Laravel Eloquent ORM - Removing rows and all the inner relationships
回答by Shaz Amjad
Just a quick recap:
简单回顾一下:
$model->related_model
will return the related model.$model->related_model()
will return the relation object.
$model->related_model
将返回相关模型。$model->related_model()
将返回关系对象。
You can do either $model->related_model->delete()
or $model->related_model()->get()->delete()
to access the delete()
method on the model.
您可以执行$model->related_model->delete()
或$model->related_model()->get()->delete()
访问delete()
模型上的方法。
Another way of handling the deletion of related (or sub) models is to use foreign key constraints when you write your migrations, check https://laravel.com/docs/master/migrations#foreign-key-constraints
处理相关(或子)模型删除的另一种方法是在编写迁移时使用外键约束,检查https://laravel.com/docs/master/migrations#foreign-key-constraints