How do I save multiple checkbox values in a single column in the database and retrieve it using laravel

10,069

Solution 1

You're creating multiple arrays by doing this

{{Form::checkbox('art[]','art')}}Art

Insted of art[] use seomthing like hobby[] and then put art as the value

Try this

    <label for="art" class="checkbox-inline">
        {{Form::checkbox('hobby[]','art')}}Art
     </label>
     <label for="artitecture" class="checkbox-inline">        
          {{Form::checkbox('hobby[]','artitecture')}}Artitecture
     </label>
      <label for="business" class="checkbox-inline">
          {{Form::checkbox('hobby[]','business')}}Business
      </label>
          ...
      <div class="form-group">
          {{Form::submit('ADD',['class'=>'form-control'])}} 
      </div>

And then in your controller you would go something like

foreach ($request->input("hobby") as $hobby){
        $add_hobby = new Hobbies;
        $add_hobby->name= $hobby;
        $add_hobby->save();
}

Solution 2

You could do a simple step to save this in a one shot.

 <label for="art" class="checkbox-inline">
    {{Form::checkbox('art','art')}}Art
 </label>
 <label for="artitecture" class="checkbox-inline">        
      {{Form::checkbox('artitecture','artitecture')}}Artitecture
 </label>
  <label for="business" class="checkbox-inline">
      {{Form::checkbox('business','business')}}Business
  </label>
      ...
  <div class="form-group">
      {{Form::submit('ADD',['class'=>'form-control'])}} 
  </div>

And then in your controller,

$hobby = implode(",",array_keys($request->except(['_method','_token'])))
//Exclude the parameters from the $request using except() method
//now in your $hobby variable, you will have "art,artitecture,business"

$add_hobby=new Hobbies;
$add_hobby->name=$hobby;
$add_hobby->save();

Dont forget to exclude the data that you dont need for hobbies.

Share:
10,069
Siros Fakhri
Author by

Siros Fakhri

Updated on June 04, 2022

Comments

  • Siros Fakhri
    Siros Fakhri almost 2 years

    I have 15 Checkbox at my admin panel so only website admin can select or cancel them.

    I need to save checkbox's that checked at my table like this:

    Name: car,food,game,...
    

    HTML:

    {{Form::open(['action'=>'adminHobbyController@store'])}}
        <div class="form-group">
            <label for="art" class="checkbox-inline">
                {{Form::checkbox('art[]','art')}}Art
             </label>
             <label for="artitecture" class="checkbox-inline">        
                  {{Form::checkbox('artitecture[]','artitecture')}}Artitecture
             </label>
              <label for="business" class="checkbox-inline">
                  {{Form::checkbox('business[]','business')}}Business
              </label>
                  ...
              <div class="form-group">
                  {{Form::submit('ADD',['class'=>'form-control'])}} 
              </div>
     {{Form::close()}}
    

    My Controller Store Function :

     public function store(Request $request)
     {
        $add_hobby=new Hobbies;
        $add_hobby->name=$request->all();
        $add_hobby->save();
        return redirect()->back();
     }
    

    Also try this but only save the last one :

    public function store(Request $request)
     {
        $add_hobby=new Hobbies;
        $add_hobby->name=$request->input('car');
        $add_hobby->name=$request->input('food');
          ...
       $add_hobby->name=$request->input('fashion');
        $add_hobby->save();
        return redirect()->back();
     }
    

    I tried this too but I got Error :

     public function store(Request $request)
    {
        $request->merge([
        'name' => implode(',', (array) $request->input('game')),
        'name' => implode(',', (array) $request->input('food')),
          ...
          'name' => implode(',', (array) $request->input('fashion')),
    ]);
    
        $add_hobby=new Hobbies;
        $add_hobby->name=$request->input()->all();
        $add_hobby->save();
        return redirect()->back();
    }
    

    Anyone can help?

    Of course is not necessary save at one column but also i don't know another way to save them