我正在尝试从我的数据库中删除品牌和供应商,它们彼此相关并且品牌与产品相关,我在进行最终删除之前删除了这些关系(至少我认为我是)并且我收到此错误,我不确定我错过了什么。最初在品牌模型中与产品没有关系,关系在产品模型中。我在没有运气的情况下将关系添加到品牌模型,结果仍然相同。
表结构
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 products() {
return $this->hasMany(Product::class);
}
}
class Product extends Products
{
public function brand()
{
return $this->belongsTo(Brand::class);
}
}
销毁控制器中的函数
叮当猫咪
喵喵时光机
www说