Laravel同步产品与分类、子分类时报subcategory_id无默认值如何解决
问题根因
报错是因为中间表设计、关联定义多处不规范导致的,具体问题如下:
category_products中间表设置了product_id、category_id、subcategory_id三个非空字段,且未设置默认值,但你用两个独立的多对多关联分别操作该表,每次同步只会传入对应关联的2个字段,剩余字段无值触发1364错误Product模型中subcategory关联的外键顺序写反:belongsToMany第三个参数是当前模型(Product)在中间表的外键,第四个是关联模型(SubCategory)在中间表的外键,你填写的'subcategory_id','category_id'完全不符合规则Subcategory模型的products关联指定的中间表是subcategory_products,和Product模型中用的category_products不一致,两边关联对应不上category_products表的subcategory_id外键错误关联到了categories表,应该关联子分类表才对
解决方案(推荐)
按照Laravel多对多关联的规范拆分中间表,不要把分类、子分类两个独立关联塞到同一个中间表中:
- 调整
category_products表迁移,删除subcategory_id相关定义:
$table->unsignedBigInteger('product_id'); $table->unsignedBigInteger('category_id'); $table->foreign('product_id') ->references('id') ->on('products') ->onDelete('cascade'); $table->foreign('category_id') ->references('id') ->on('categories') ->onDelete('cascade'); $table->primary(['category_id','product_id']);
- 新建
subcategory_products中间表,迁移代码如下:
$table->unsignedBigInteger('product_id'); $table->unsignedBigInteger('subcategory_id'); $table->foreign('product_id') ->references('id') ->on('products') ->onDelete('cascade'); // 注意此处关联的表名是你实际的子分类表名,若有不同自行调整 $table->foreign('subcategory_id') ->references('id') ->on('sub_categories') ->onDelete('cascade'); $table->primary(['subcategory_id','product_id']);
- 修正模型关联定义
- Product模型修正:
public function cats(){ return $this->belongsToMany(Category::class, 'category_products', 'product_id', 'category_id'); } // 方法名调整为复数更符合规范,外键顺序修正 public function subcategories() { return $this->belongsToMany(SubCategory::class, 'subcategory_products', 'product_id', 'subcategory_id'); }
- Subcategory模型修正:
public function products() { // 外键顺序修正,模型名统一为单数Product return $this->belongsToMany(Product::class,'subcategory_products', 'subcategory_id', 'product_id'); } public function category(){ return $this->belongsToMany(Category::class)->withTimestamps(); }
- 控制器代码调整调用方法名即可:
$data->cats()->sync($request->cats,false); $data->subcategories()->sync($request->subcategory,false);
内容的提问来源于stack exchange,提问作者مهدی ذوالفقاری
相关产品推荐
相关产品推荐

