I have 2 models with Many-to-Many Relation when I create new Device and check multiple Checkboxes I want to save multiple voices_id with Device_id into the Pivot Table device_voice
Device Controller
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();
$device = $this->deviceRepository->create($input);
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();
$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);
}
Thanks!
via Ahmed Abdulrahman