I have the following rules in my From Request:
return [
'brand_id' => 'required|numeric',
'name' => 'required|string',
'wage_type_id' => 'required|numeric',
'work_days' => 'required|array',
'weekly_min' => 'required|numeric',
'weekly_max' => 'required|numeric',
'weekly_utmost' => 'required|numeric',
'daily_std' => 'required|numeric',
'daily_min' => 'required|numeric',
'daily_max' => 'required|numeric',
];
One of them, work_days is supposed to be sent as an array.
I could create the work type from validated form request like
WorkType::create($request->all());
BUT I cannot store an array in database for sure.
Is there a way to get work_days imploded so that I can use the one line above to create my record from the request directly?
Note: I want the array imploded, not json encoded, because I'm going to insert into a field type of SET.
You can actually modify the request params before creating the model in your controller.
$request->merge([
"work_days" => implode(", ", $request->work_days)
]);
WorkType::create($request->all());
If you want it to be in the FormRequest
, override the passedValidation()
method
class SampleFormRequest extends FormRequest
{
public function passedValidation()
{
$this->request->add([
"work_days" => implode(", ", $this->request->get("work_days"))
]);
}
}
But for me, I prefer writing all properties separately and not using the $request->all()