删除特定组后,我想删除库存表中的ImageView
。
表格结构:
product_groups
id | grp_name产品
id | prod_name |价格| GROUP_ID库存
id | product_id | IN_STOCK
代码:
product_id
模型
答案 0 :(得分:1)
1)您可以在库存迁移中使用它:
$table->foreign('product_id')->references('id')->on('products')->onDelete('cascade');
来源:https://laravel.com/docs/5.5/migrations#foreign-key-constraints
2)您可以覆盖删除方法:
class Product extends Model {
protected $table = 'products';
public function inventory(){
return $this->hasMany('App\Inventory', 'product_id');
}
public function delete()
{
DB::transaction(function()
{
$this->inventory()->delete();
parent::delete();
});
}
}