How the send multiple checkbox values to database in laravel?
16,880
Make a text column in your table with the name of favorite
and use this logic to store your values inside of it as csv
public function store(Request $request)
{
$request->merge([
'favorite' => implode(',', (array) $request->get('favorite'))
]);
laravel::create($request->all());
return "data saved";
}
Author by
jvk
Updated on June 04, 2022Comments
-
jvk almost 2 years
This is my view for the customer register.
<form action="store" method="post"> <input type="hidden" name="_token" value="{{csrf_token()}}"> <label for="name">Name</label> <input type="text" name="name"> <br> <label for="email">Email</label> <input type="text" name="email"> <br> <label for="country">Country</label> <select name="country" id="country"> <option value="india">India</option> <option value="srilanka">SriLanka</option> <option value="usa">USA</option> </select> <br> <input type="radio" name="gender" value="male"> <label for="male">Male</label> <input type="radio" name="gender" value="female"> <label for="female">Female</label> <br> <input type="checkbox" name="favorite[]" id="south" value="south"> <label for="south">South</label> <input type="checkbox" name="favorite[]" id="north" value="north"> <label for="north">North</label> <input type="checkbox" name="favorite[]" id="east" value="east"> <label for="east">East</label> <br> <label for=""></label> <input type="submit" name="submit" value="Submit"> </form>
Well all values are going to database but the checkbox are going as array.
But if i remove [] in favorite. The last checkbox value is going to database.
This is my controller code
public function store(Request $request) { $user= laravel::create(Request::all()); return "data saved"; }
And this is my model
class laravel extends Model { protected $fillable = [ 'name', "email", "gender", "country", "favorite" ]; }
Can any one tell me how to send all checkbox values to database whatever customer is checked.
I want to know how to edit checkbox to update.
Thank you in advance.