You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel批量提交表单时gender与other_name数据被覆盖问题求助

问题:好友套餐信息被最后一条数据覆盖,字段无法关联对应用户

我在bravo_book_others数据表新增了gender和other_name字段,业务场景是用户user1为自己和两位好友购买3份套餐。

输入表单数据

Name : user1
Email : user1@gmail.com

Gender : male
Name : a
Email : a@gmail.com

Gender : female
Name : b
Email : b@gmail.com

实际输出(异常)

Name : user1
Email : user1@gmail.com

Gender : female
Name : b
Email : a@gmail.com

Gender : female
Name : b
Email : b@gmail.com

可见除主用户外,其余用户的gender和other_name字段被最后一条输入的数据覆盖。


现有代码

Controller代码

foreach ($request->input('other_emails') as $email){
            BookOther::create([
                'booking_id'=>$booking->id,
                'user_id'=>$booking->customer_id,
                'gender'=>$request->input('gender'),
                'other_name'=>$request->input('other_name'),
                'other_emails'=>$email
            ]);
            Mail::to($email)->send(new OthersEmail($isi_email1));
        }

Blade模板代码

@for ($i = 1; $i < $numbers; $i++)
                <div class="col-md-12">
                        <label >Name#{{$i}} </label>
                        <input type="text" class="form-control" id="other_name" name="other_name" autocomplete="off">
                    </div>
<!--the reason i don't user other_name[]-->
<!--Because i get error message array to string conversion-->
                    <div class="col-md-12">
                        <label >Email#{{$i}} </label>
                        <input type="text" class="form-control" id="other_emails" name="other_emails[]" autocomplete="off">
                    </div>
                @endfor

此前尝试将other_name设为数组形式(other_name[])时触发了array to string conversion错误,因此未采用数组形式,现需解决数据被覆盖问题。


解决方案

1. 修改Blade模板,按用户分组字段

问题根源是other_name、gender为单值字段,提交时仅保留最后一次输入值,无法与other_emails数组对应。需将每个好友的所有字段按索引分组,确保每组对应同一个用户:

修改后的Blade模板:

@for ($i = 1; $i < $numbers; $i++)
    <div class="col-md-12">
        <label>Gender#{{$i}}</label>
        <input type="text" class="form-control" name="friends[{{$i}}][gender]" autocomplete="off">
    </div>
    <div class="col-md-12">
        <label>Name#{{$i}}</label>
        <input type="text" class="form-control" name="friends[{{$i}}][other_name]" autocomplete="off">
    </div>
    <div class="col-md-12">
        <label>Email#{{$i}}</label>
        <input type="text" class="form-control" name="friends[{{$i}}][other_emails]" autocomplete="off">
    </div>
@endfor

提交后$request->friends会成为多维数组,每个元素对应一个好友的完整信息,避免覆盖。

2. 修改Controller代码,遍历分组后的数组

改为遍历friends数组,直接获取每个好友的所有字段:

foreach ($request->input('friends') as $friend){
    // 可选:跳过空邮箱的无效数据
    if(empty($friend['other_emails'])) continue;
    
    BookOther::create([
        'booking_id' => $booking->id,
        'user_id' => $booking->customer_id,
        'gender' => $friend['gender'],
        'other_name' => $friend['other_name'],
        'other_emails' => $friend['other_emails']
    ]);
    Mail::to($friend['other_emails'])->send(new OthersEmail($isi_email1));
}

3. 解决array to string conversion错误

之前的错误是因为直接将数组赋值给数据库字符串字段,现在分组后每个$friend['other_name']都是单值字符串,不会触发转换错误。若有字段验证规则,确保每个分组字段为单值输入即可。

内容的提问来源于stack exchange,提问作者Nicho

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 17:15:37