A boolean has a true and false values, so what do you mean with three options? You should have a different check then if the value is different.
Checkbox
I am dealing with a three option checkbox. In the validator, do I do it as boolean?
'checkbox' => ['required', 'boolean', 'max:255'],
and in the store function how do I input it?
If you mean 3 options from the checkbox as in 3 different values then no, this is not a boolean. You access it just like every other value from the request. https://laravel.com/docs/master/requests#retrieving-input
'checkbox' => ['required', 'boolean', 'max:255'],
Shouldn't you be storing a 1 or 0 for a tinyint. And if not checked nothing is passed.
So by required, are you saying has to be checked.
I have a checkbox named services and then the options would be service 1, service 2, service 3. Any can be clicked or none can be clicked. I don't understand how to put it in validation and input into the database.
@artisticre Do you plan to store more than 1, like an array? [1,2]? If not you should probably use radio instead of checkboxes.
If you want to store multiple you should add [] after the name to make it an array and the validation rule would be something like this:
'services.*' => ['in:1,2,3'],
If I'm wrong about the [] please correct me anyone.
Yes I am wanting to store multiple options or none, depending on what they choose. So if I add this rule in the validator, do I add the array to the input on the form as well? Such as
name="services[]"
Rules should just be
'checkbox' => ['boolean'],
boolean is true/false or 0/1. No point in also saying max:255 when it can already be only a 0/1.
To get value, just
$value = $request->has('checkbox');
will be a 1 if they checked it, or a 0 if they didn't (due to has() returning a boolean)
But if there are multiple checkboxes, it wouldn't be checked or not checked?
If this is a checkbox array, you have to Loop over it and see what was checked and not checked.
In other words the values that are in the loop were checked.
If you know which ones are checked (they come through in the request), wouldn't you also know that the others are 0's (not checked) by process of elimination? You could use array_diff() or something to determine which ones were not checked based on which ones are checked. Here's a similar answer: https://laracasts.com/discuss/channels/laravel/how-i-save-data-from-multiple-select-to-database-column-type-json
Please or to participate in this conversation.