php 将多个选择ID存储到数据库

ylamdve6  于 2023-03-11  发布在  PHP
关注(0)|答案(1)|浏览(148)

因此,我想将下拉菜单中的多项选择与ID值存储到数据库中,但我收到错误代码无效文本表示:7错误:类型bigint的输入语法无效:“1,2”(SQL:插入到“组_客户”(“组_名称”,“成员_客户”,“活动”,“状态”,“描述”,“更新_时间”,“创建_时间”)值(组1,1,2,是,?,?,2023-03-10 10:02:49,2023-03-10 10:02:49)返回“id”)
以下是我看法

<div class="row mb-3">
                    <label for="member_customer" class="col-sm-4 col-form-label">Member</label>
                    <div class="col-sm-8">
                        <select id="member_customer" class="form-select show-tick ms" data-placeholder="Select" name="member_customer[]" multiple>
                            @foreach ($customer as $data)
                                <option value="{{ $data->id}}">{{ $data->name_customer }}</option>
                            @endforeach
                        </select>
                    </div>
                    @if ($errors->has('member_customer'))
                        <span class="text-danger">* {{ $errors->first('member_customer') }}</span>
                    @endif
                </div>

控制器

public function addgroup(Request $request){
        $request->validate([
            'group_name' => 'required',
            'member_customer'=> 'required',
            'active' => 'nullable',
            'status' => 'nullable',
            'description' => 'nullable',

        ]);

        $data = new Group_customer();
        $data->group_name = $request->group_name;
        $data->member_customer = implode(',',$request->member_customer);
        $data->active = $request->active;
        $data->status = $request->status;
        $data->description = $request->description;
        $data->save();

        return redirect('admin/groupcustomer')->with('success','Data Added Successfully');
    }

模型

class Group_customer extends Model
{
    use HasFactory;
    public $table='group_customer';
    protected $primaryKey = 'id';
    protected $fillable = [
        'id',
        'group_name',
        'member_customer',
        'active',
        'status',
        'description',
    ];

    public function Customer()
    {
        return $this->hasMany(Customer::class, 'member_customer');
    }
}

迁移

public function up()
    {
        Schema::create('group_customer', function (Blueprint $table) {
            $table->increments('id');
            $table->string('group_name');
            $table->foreignId('member_customer')->nullable();
            $table->foreign('member_customer')->references('id')->on('customer')->onUpdate('cascade')->onDelete('set null')->nullable();
            $table->string('active')->nullable();
            $table->string('status')->nullable();
            $table->string('description')->nullable();
            $table->timestamps();
        });
    }
9lowa7mx

9lowa7mx1#

因为member_customer的类型是bigint,所以下面的语句是错误的

$data->member_customer = implode(',',$request->member_customer);

如果要将每个member_customer插入到每条记录中,可以考虑执行以下操作:

$data = [];
foreach ($request->member_customer as $customer_id) {
    $data[] = [
         "group_name" => $request->group_name,
         "member_customer" => customer_id,
         "active" => $request->active,
          "status" => $request->status,
         "description" => $request->description
    ]
}

Group_customer::insert($data)

相关问题