Search code examples
laravelinfyom

How to save multi selected values?


I a have a one to many relationship between culprits and crimes and also a one to many relationship between species and crime.

culripts id first_name last_name

crimes id culprit_id species_id fishing_method_id

species id species_name

fishing_methods id fishing_methods_name

What I need to achieve is to save many species for one crime

my form

   {{ Form::select('species_id',$speciesRepository, array('Action'), array('multiple', 'class' =>'form-control')) }}

my controller

   public function store(CreateCrimeRequest $request)
     {
         $input = $request->all();

         // $data = implode(',', $input);

        // dd($data);

          $crime = $this->crimeRepository->create($data);

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

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

When I select 3 species from the dropdown the last selected species is saved while the other two are not. could anyone help to solve this


Solution

  • You are missing [ ] in your select name attribute

    {{ Form::select('species_id[]', $speciesRepository, array('Action'), array('multiple', 'class' =>'form-control')) }}