Laravel中如何向pivot table(中间透视表)保存多字段数据
Laravel 多对多中间表额外字段存储修复
当前代码存在3个会导致qte、total字段无法正确写入中间表的问题:
- 多对多关联定义不完整:两个模型主键均非Laravel默认的
id字段,未显式指定关联外键会导致ORM按默认规则生成错误的外键名;重复调用withPivot的写法虽然不报错,但规范写法是一次性传入所有中间表字段 attach方法传参错误:中间表的额外字段需要统一放在第二个参数的数组中传入,拆分到第三个数组的字段不会被写入中间表- 未做空值兼容处理,当提交的数量、总价字段缺失时会出现数组下标不存在的报错
修正步骤
1. 完善Devis模型的关联定义
belongsToMany方法需要依次指定:关联模型类、中间表名、当前模型在中间表的外键、关联模型在中间表的外键,同时一次性声明所有中间表额外字段:
class Devis extends Model { protected $table = 'Devis'; protected $primaryKey = 'idd'; protected $fillable = ['type', 'lot', 'prix_total_avant_tax', 'tax_perse', 'tax_amount', 'prix_total_apre_tax', 'notes']; public function articals() { return $this->belongsToMany(Artical::class, 'devis_artical', 'idd', 'ida') ->withPivot(['qte', 'total']); } }
如果需要在Artical模型中反向查询关联的Devis,按相同规则指定外键定义关联即可。
2. 修正store方法的attach逻辑
修正传参格式,增加空值兼容,建议补充基础表单校验避免非法数据写入:
public function store(Request $request) { // 基础校验,可根据业务需求调整规则 $validated = $request->validate([ 'type' => 'required', 'lot' => 'required', 'prix_total_avant_tax' => 'required|numeric', 'articals' => 'array', 'quantité' => 'array', 'total' => 'array' ]); $devis = Devis::create($validated); $articals = $request->input('articals', []); $quantite = $request->input('quantité', []); $total = $request->input('total', []); for ($product = 0; $product < count($articals); $product++) { if (empty($articals[$product])) { continue; } // 所有中间表字段合并为一个数组传入第二个参数 $devis->articals()->attach($articals[$product], [ 'qte' => $quantite[$product] ?? 0, 'total' => $total[$product] ?? 0 ]); } return redirect('/Devis'); }
性能优化写法
可以提前组装关联数据,批量执行attach减少SQL查询次数:
$attachData = []; for ($product = 0; $product < count($articals); $product++) { if (empty($articals[$product])) { continue; } $attachData[$articals[$product]] = [ 'qte' => $quantite[$product] ?? 0, 'total' => $total[$product] ?? 0 ]; } if (!empty($attachData)) { $devis->articals()->attach($attachData); }
完成以上修改后,qte和total字段就可以正确写入devis_artical中间表。
内容的提问来源于stack exchange,提问作者bakir_nano
相关产品推荐
相关产品推荐

