When a checkbox is ticked, it's value is present in the posted data. When it is unticked, it's value is not present. This means when you do $request->all()
it will contain only the checkboxes that were ticked. So in your case, if you leave all 3 checkboxes unticked, your $request->all()
could yield an empty array (Assuming that no other fields are posted).
When you run Article::create($request->all());
with no checkboxes ticked you would be essentially passing it an empty set of data, which means that your database is going to be populated with the default values for the fields that you didn't provide.
Since you didn't provide a default in your migration, MySQL is going to guess what the default should be based on the field type, which in the case of a boolean will be 0
. You may see some warnings/errors though.
There are loads of ways you can get this to work so that a 1
is saved when a checkbox is ticked or 0
when it is not ticked. The easiest way in your scenario would be to set the value of each checkbox to be 1
and explicitly set up default values in your migration i.e.
Migration:
$table->boolean('favicon')->default(0);
Form:
{!! Form::checkbox('title', '1'); !!}
This way, when the title checkbox is ticked, your $request->all()
returns an array containing an item 'title' => '1'
and this is saved in your database. All of the unticked boxes are defaulted to 0 as per your migration.
I prefer however to be more explicit when I write my method for handling the store.
$article = new Article();
$article->title = $request->has('title'); // Will set $article->title to true/false based on whether title exists in your input
// ...
$article->save();