3 回答

TA贡献1776条经验 获得超12个赞
如果要在删除记录时始终删除所有子关系,可以在模型的 boot 函数中的删除方法中进行。像这样的东西:
供应商模型
class Vendor extends Model
{
public static function boot() {
parent::boot();
// when you are deleting a Vendor, also delete all related brands
static::deleting(function($vendor){
$vendor->brands->each(function($brand) {
$brand->delete();
});
});
}
protected $hidden = ['created_at','updated_at'];
public function brands(){
return $this->hasMany(Brand::class);
}
}
品牌型号
class Brand extends Model
{
public static function boot() {
parent::boot();
// when you are deleting a Brand, also delete all related products
static::deleting(function($brand){
$brand->products->each(function($product) {
$product->delete();
});
});
}
public function vendor() {
return $this->belongsTo(Vendor::class);
}
public function products() {
return $this->hasMany(Product::class);
}
}
产品型号
class Product extends Products
{
public static function boot() {
parent::boot();
// when you are deleting a Product, also delete/detach all you need
static::deleting(function($product){
/*
$product->sizes()->detach();
$product->tags()->detach();
$product->fields()->detach();
$product->countries()->detach();
$product->exportationFactors->each(function($exportationFactor) {
$exportationFactor->delete();
});
*/
});
}
public function brand()
{
return $this->belongsTo(Brand::class);
}
}
然后在您的控制器中删除与每个控制器对应的记录。
供应商销毁功能
public function destroy($id)
{
DB::beginTransaction();
Vendor::findOrFail($id)->delete();
DB::commit();
}
品牌破坏功能
public function destroy($id)
{
DB::beginTransaction();
Brand::findOrFail($id)->delete();
DB::commit();
}
产品销毁功能
public function destroy($id)
{
$product = Product::findOrFail($id);
DB::beginTransaction();
$this->removeProductImage($product);
$product->delete();
DB::commit();
}

TA贡献1846条经验 获得超7个赞
添加你拥有的onDelete('cascade')
每一个$table->foreign('<Column>')
。
例子:
$table->foreign('vendor_id')->references('id')->on('vendors')->onDelete('cascade');
然后不需要先删除所有的孩子,只需删除父母。

TA贡献1775条经验 获得超8个赞
您遇到的问题在产品表中,有两种方法可以解决此问题:
解决方案1:
就像 Yovi 的回答状态一样,您可以简单地onDelete('cascade')在您的品牌和产品表中添加您的外键。
品牌表:
$table->foreign('vendor_id')->references('id')->on('vendors')->onDelete('cascade');
产品表:
$table->foreign('brand_id')->references('id')->on('brands')->onDelete('cascade');
然后您的控制器销毁方法应如下所示:供应商销毁功能:
public function destroy($id)
{
$vendor = Vendor::findOrFail($id);
$vendor->delete();
}
品牌销毁方法:
public function destroy($id)
{
$brand= Brand::findOrFail($id);
$brand->delete();
}
解决方案2:
如果你想手动删除你的行,你只是在你的销毁方法上设置了错误的顺序。您必须首先从产品 -> 品牌 -> 供应商开始删除最小的孩子。您的方法应如下所示:
供应商销毁功能:
public function destroy($id)
{
DB::beginTransaction();
$vendor = Vendor::findOrFail($id);
foreach($vendor->brands() as $brand){
$brand->products()->delete();
}
$vendor->brands()->delete();
$vendor->delete();
DB::commit();
}
品牌销毁功能:
public function destroy($id)
{
DB::beginTransaction();
$brand= Brand::findOrFail($id);
$brand->products()->delete();
$brand->delete();
DB::commit();
}
总的来说,我发现解决方案 1 更清洁。
- 3 回答
- 0 关注
- 193 浏览
添加回答
举报