Normally, you could update a model using the validated data from a form request:
public function update(Item $item, StoreItem $request)
{
$item->update($request->validated());
return redirect()->back();
}
However, occassionally, I might want to validate data that is part of the model, but also data that isn't part of the model. For example, let's say I had a tick box called "I agree to updating these details" and I want to validate that the tick box is checked, but this isn't part of the model.
To do that, I have to resort to the following:
public function update(Item $item, StoreItem $request)
{
$data = collect($request->validated())->forget('tickbox_agree')->toArray();
$item->update($data);
return redirect()->back();
}
Basically, I convert $request->validated from an array to a collection, remove the element in question, then convert it back to an array. It works, but is there a better way?
Thanks,