【发布时间】:2020-12-02 12:23:02
【问题描述】:
我的问题与 Laravel 有关,我有 product 模型和 categories。他们与category_id 有关系,作为product 表的外键,我的问题是,我需要删除一个类别时,这个category_id 相关字段是NULL,我的意思是,有一个Laravel 方法可以做它?没有在migration.
这是我的类别模型:
public function products()
{
return $this->hasMany('App\Models\Product');
}
这是我的产品型号:
public function category()
{
return $this->belongsTo('App\Models\Category')->withDefault(function ($data) {
foreach($data->getFillable() as $dt){
$data[$dt] = __('Deleted');
}
});
}
【问题讨论】:
-
当你现在删除一个类别会发生什么?
-
该类别已删除,但在我的产品表中,category_id 仍然存在!我应该是 NULL
-
那么,没有设置外键?更有理由进行迁移更新。
-
外键在我的产品表中,但当我删除一个类别时应该为空。
标签: mysql laravel model-view-controller eloquent