为什么不能从具有关系的表中删除它? [英] Why can't I delete this from my tables that have relationships?

查看:76
本文介绍了为什么不能从具有关系的表中删除它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么我不能从数据库中删除这些项目,它们每个都只有一种关系,而我却分别分离和删除它们.我有3个表供应商,品牌和产品.该错误提及产品"表,但供应商"表与产品"表之间没有任何关系,该关系与品牌"表有关,并且是一对一的关系.

Why can't I delete these items from my database, they only have one relationship each, and I'm detaching and deleting each respectively. I have 3 tables Vendor, Brand and Product. The error mentions the Products table but there are no relationships in the Vendor table to the Products table, the relationship is with the Brand table and it's a one to one relationship.

表结构

Schema::create('vendors', function (Blueprint $table)
        {
            $table->increments('id');
            $table->string('name');
            $table->string('image')->nullable();
            $table->timestamps();
        });

Schema::create('brands', function (Blueprint $table) {
            $table->increments('id');
            $table->string('name')->nullable();
            $table->integer('vendor_id')->unsigned();;
            $table->foreign('vendor_id')->references('id')->on('vendors');
            $table->timestamps();
        });

Schema::create('products', function (Blueprint $table) {
            $table->increments('id');
            $table->string('code');
            $table->string('sku')->nullable();
            $table->text('description_spanish');
            $table->text('description_english');
            $table->string('image')->nullable();
            $table->string('discount');
            $table->string('cif')->nullable();
            $table->string('color')->nullable();
            $table->string('color_ab')->nullable();
            $table->integer('brand_id')->unsigned();
            $table->timestamps();

            $table->foreign('brand_id')->references('id')->on('brands');
        });

供应商模型和关系

class Vendor extends Model
{
    protected $hidden = ['created_at','updated_at'];

    public function  brands(){
        return $this->hasMany(Brand::class);
    }
}

品牌模型和关系

class Brand extends Model
{
    public function vendor() {
        return $this->belongsTo(Vendor::class);
    }
}

与品牌的产品关系

public function brand()
    {
        return $this->belongsTo(Brand::class);
    }

供应商销毁功能

public function destroy($id)
    {
        DB::beginTransaction();
        $vendor = Vendor::findOrFail($id);
        $vendor->brands()->delete();
        $vendor->delete();
        DB::commit();

    }

品牌销毁功能

public function destroy($id)
    {
        DB::beginTransaction();
        $vendor = Brand::findOrFail($id);
        $vendor->vendors()->delete();
        $vendor->delete();
        DB::commit();
    }

产品破坏功能

public function destroy($id)
    {
        $product = Product::findOrFail($id);
        DB::beginTransaction();
        $product->sizes()->detach();
        $product->tags()->detach();
        $product->fields()->detach();
        $product->countries()->detach();
        $this->removeProductImage($product);
        $product->exportationFactors()->delete();
        $product->delete();
        DB::commit();
    }

当我尝试删除供应商时,我收到此错误

when I try to delete a Vendor I recieve this error

SQLSTATE[23000]: Integrity constraint violation: 
1451 Cannot delete or update a parent row: 
a foreign key constraint fails (`sondel`.`products`, 
CONSTRAINT `products_brand_id_foreign`FOREIGN KEY (`brand_id`) 
REFERENCES `brands` (`id`)) (SQL: delete from `brands` where 
`brands`.`vendor_id` = 2 and `brands`.`vendor_id` is not null)

当我尝试删除品牌时,我基本上有相同的错误

and when I try to delete a Brand I have basically the same error

SQLSTATE[23000]: Integrity constraint violation: 
1451 Cannot delete or update a parent row: 
a foreign key constraint fails (`sondel`.`brands`, CONSTRAINT
 `brands_vendor_id_foreign` FOREIGN KEY (`vendor_id`) 
REFERENCES `vendors` (`id`)) (SQL: delete from `vendors` 
where `vendors`.`id` = 2)

我不确定自己在做什么错.

I'm not sure what I'm doing wrong.

推荐答案

供应商删除错误是因为您要删除与产品相关的供应商所有品牌,因此,如果不删除其相关产品,就无法删除它们. 因此,您必须首先删除产品.在品牌模型上创建产品关系

Error on Vendor Delete is Because You are deleting Vendor's all Brands which are again related to Products So, without deleting their related products you can't delete them. So, you have to delete products at first. Create a relation of Products on Brand Model

public function  products(){
    return $this->hasMany(Products::class);
}

然后在供应商销毁的情况下,让所有品牌循环浏览并删除所有产品.

Then on Vendor's destroy get its all brands loop through and delete all products.

DB::beginTransaction();
$vendor = Vendor::findOrFail($id);
$vendor->brands->each(function ($brand) {
    $brand->products->each(function ($product) {
         $product->sizes()->detach();
         $product->tags()->detach();
         $product->fields()->detach();
         $product->countries()->detach();
         $this->removeProductImage($product);
         $product->exportationFactors()->delete();
         $product->delete();
    });
});
$vendor->brands()->delete();
$vendor->delete();
DB::commit();

Same On Brand的删除将获取其所有产品,然后先删除其产品.

Same On Brand's delete get all its products and delete its products first.

DB::beginTransaction();
$brand = Brand::findOrFail($id);
$brand->products->each(function ($product) {
     $product->sizes()->detach();
     $product->tags()->detach();
     $product->fields()->detach();
     $product->countries()->detach();
     $this->removeProductImage($product);
     $product->exportationFactors()->delete();
     $product->delete();
});
$brand->delete();
DB::commit();

我建议您对所有三个模型都使用Eloquent的deleteing事件,这样您就不必关心嵌套模型的外键,并且可以摆脱代码重复.请参阅: https://laravel.com/docs/6.0/eloquent#events

I would like to suggest you to use Eloquent's deleting event on all three models then you will not need to care about nested model's foreign key and you will get rid of code repitation. see: https://laravel.com/docs/6.0/eloquent#events

这篇关于为什么不能从具有关系的表中删除它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆