Answer the question
In order to leave comments, you need to log in
How to set default value of model attribute in update process with fillable fields?
Greetings all
I can not find a solution to the problem.
In general, there is a field in the checkbox form with the name "published" and the value 1, if it is in the "checked" state, it is logical that the number 1 is sent to the controller update and this code works fine
public function update(Request $request, $id)
{
$this->model->find($id)->update($request->all());
return back();
}
Answer the question
In order to leave comments, you need to log in
but I would not want to put the save/create/update logic into the controller
1) Instead of $request->all(), it's better to use $request->only(['required', 'fields', 'published'])
2) Mutators work only with data received from the database.
3) The simplest and most correct option is to specify the default value == 0 in the migration. Because the value can be either 0 or 1 and no other. And even if published data does not come from the frontend at all, save will work correctly without an error from PDO$table->integer('published')->default(0);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question