【问题标题】:How to save multiple Checkboxes value into Pivot Table?如何将多个复选框值保存到数据透视表中?
【发布时间】:2017-03-01 13:07:46
【问题描述】:

当我创建新设备并选中多个复选框时,我有 2 个具有多对多关系的模型我想将多个带有 Device_id 的 voices_id 保存到数据透视表中 device_voice

设备控制器(我面临这个错误未定义变量:编辑功能中的 selectedVoice)

public function create()
{
    $this->authorize("create", Device::class);

    $voice_list = Voice::all();

    return view('devices.create')
        ->with('voice_list', $voice_list);
}

public function store(CreateDeviceRequest $request)
{
    $this->authorize("create", Device::class);

    $input = $request->all();
    $voices_checkbox = $input['voice'];

    $device = $this->deviceRepository->create($input);

    //Device Id and Selected voices is Saved Successfully 
    $device ->voices()->sync($voices_checkbox);

    Flash::success('Device saved successfully.');

    return redirect(route('devices.index'));
}
public function edit($id)
{
    $device = $this->deviceRepository->findWithoutFail($id);

    $this->authorize("update", $device);

    $voice_list    = Voice::all();

    // Here I am getting Device's checked voices, but sinces I am sharing      the same Fields Blade with Create and Edit I get this error 
    //Undefined variable: selectedVoice 
    $selectedVoice = $device->voices->pluck("id")->toArray();

    if (empty($device)) {
        Flash::error('Device not found');

        return redirect(route('devices.index'));
    }

    return view('devices.edit')
          ->with('device', $device)
          ->with('voice_list', $voice_list)
          ->with('selectedVoice', $selectedVoice);
}

这是我的创建和编辑视图的字段

<!-- Device Number Field -->
<div class="form-group col-sm-6">
    {!! Form::label('device_number', 'Device Number:') !!}
    {!! Form::text('device_number', null) !!}
</div>

<!-- Batch Field -->
<div class="form-group col-sm-6">
    {!! Form::label('device_name', 'Device Name:') !!}
    {!! Form::text('device_name', null) !!}
</div>

<!-- Batch Field -->
<div class="form-group col-sm-6">
    {!! Form::label('voices_id', 'Voices:') !!}

    @foreach ($voice_list as $voice)
        {!! Form::checkbox('voice[]', $voice->id, in_array($voice->id, $selectedVoice)) !!}
        {!! Form::label('voice', $voice->name) !!}
    @endforeach

</div>

<!-- Version Field -->
<div class="form-group col-sm-6">
    {!! Form::label('version', 'Version:') !!}
    {!! Form::text('version', null) !!}
</div>

谢谢!

【问题讨论】:

  • 你不能把它们都放在它们自己的形式中吗?这样你就可以在 laravel 中一一处理它们。您可以使用 javascript (ajax) 同时提交它们
  • @thomas-moors 我也解决了,但是还有另一个问题我很头疼呵呵 :D 你能再检查一下我的代码吗 :)
  • 您能否更好地描述您提供的输入以及您期望发生的情况。 “我想将多个带有 Device_id 的 voices_id 保存到数据透视表 device_voice 中”对我来说不清楚。
  • " 我想将多个带有 Device_id 的voices_id 保存到数据透视表中 device_voice " 这个已经解决了,你能检查一下 Create and Edit Functions with the Fields :) @thomas-moors
  • 您访问的网址是否正确? devices/createdevices//{device-id}/edit?你的路线注册对了吗?

标签: php mysql laravel eloquent


【解决方案1】:

使用sync() 方法:

$device->voices()->sync($request->voices);

【讨论】:

  • 现在它将语音 id 与设备 id 一起保存,但还有另一个问题是我正在为创建和编辑共享相同的字段刀片文件,而不是保存选定的复选框,为什么?我会更新我的代码@alexey-mezenin
猜你喜欢
  • 2021-12-07
  • 2017-09-05
  • 1970-01-01
  • 1970-01-01
  • 2019-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多