dipherent's avatar

What is the best way to Insert Multiple Selected Values in Laravel?

Hi friends,

I have two tables: Staff and Services. Any staff member can handle many services, and any service can be done by any number of staff.

I created a pivot table for both called staff_services

What is the best way to do the following:

When creating a new staff member, I want to be able to choose multiple services from a dropdown select field or checkboxes and assign them to the staff.

I have been doing some research, but what I found is a bit complicated and involve JS.. I am sure there is an easier way to do this but I don't seem to able to see it.

Can anyone help please?

Thanks

0 likes
6 replies
tykus's avatar

You have a multiple select dropdown with all of the possible services, and the user selects some of those services to assign to the Staff member:

<select name="services[]" multiple>
    @foreach($services as $service)
        <option value="{{$service->id}}">{{$service->name}}</option>
    @endforeach
</select>

Now, you will have an array of service ids in the Request which you can sync with the newly created Staff using the services belongsToMany relation:

$staff->services()->sync($request->input('services'));
// Staff
public function services()
{
    return $this->belongsToMany(Service::class, 'staff_services');
}
Nakov's avatar

You just list the services and attach all selected once to the new staff member.

Here is an article that can help: https://medium.com/@afrazahmad090/laravel-many-to-many-relationship-explained-822b554c1973

If you go with the checkbox option you just do this:

@foreach($services as $service)
<input name="services[]" value="{{ $service->id }}" /> {{ $service->name }} 
@endforeach

and then just get the selected services in the controller $request->input('services') and attach them to the newly created staff member.

dipherent's avatar

Thank you @tykus @nakov ... I should have mentioned I am doing this with Livewire... her is my livewire controller:

 public $name;
 public $selection=[];

        
 public function render()
    {
        return view('livewire.add-staff', [
            'services' => Service::all()
        ]);
    }

  public function storeStaff(Request $request)
    {
        $this->validate([
            'name' => 'required|min:5',
        ]);
       
        Staff::create([
            'name' => $this->name,
            
        ])->services()->sync($request->input('selection'));
        
        
        session()->flash('message', 'Staff successfully created.');
        return redirect()->to(route('dashboard'));

    }

My blade

<div>
            <label for="name">Name</label>
            <input type="text" wire:model.defer="name" name="name" id="name" >
 </div>
  <div>
            <label for="s" >Select Services</label>
            <select wire:model.defer="selection" name="s" id="s" multiple>
                @foreach($services as $service)
                    <option value="{{$service->id}}">{{$service->name}}</option>
                @endforeach
            </select>
  </div>
        
      <button type="submit" >Add Staff </button>

The staff is created with the name, but the services don't seem to be created. the table staff_service is empty.

Am I doing it right?

tykus's avatar
tykus
Best Answer
Level 104

sync($this->selection)

1 like
Nakov's avatar

Remove the $request from your method, there is no request in there, you just use the property $this->selection

1 like

Please or to participate in this conversation.