Reputation: 2323
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
Upvotes: 2
Views: 11052
Reputation: 1293
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.
Upvotes: 2
Reputation: 1026
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();
}
Upvotes: 5
Reputation: 851
First, make sure that column in which you want to store those values is varchar or text and can store all the text you'll put there.
Second, make a JSON string out from your values. (Or serialized array string but JSON is better to read). Then just store this setting as a string and do not forget to parse JSON back to an array when you access this data.
This links will help you a bit:
http://php.net/manual/en/function.json-encode.php
http://php.net/manual/en/function.json-decode.php
and for the case, you pick a seralized array: http://php.net/manual/en/function.serialize.php
Upvotes: 0
Reputation: 598
you can use the implode method and store it in a variable then assign it to the column.
$add_hobby->[column]= $array_imploded;
Upvotes: 0