In my controller I receive an Illuminate\Http\Request
Request that contains some data with values equal to an empty string:
dd($request->input())
//output
array:5 [▼
"_token" => "K43q88mR5zQRqVPAuYX5IWtQ1khQ24JTsuxl8mz4"
"param1" => "1"
"param2" => "2"
"param3" => ""
"param4" => ""
]
(basically that happens when the user had made no selections in the create form BUT due to frontend restrictions see here, I am not able to change the form of the Request so that to exclude the empty string values from being sent to the server)
The request input data is used to attach a relationships to models. For example:
$book->authors()->attach($request->input('param1'));
The problem is that when the input value is equal to the empty string ""
the above line throws a QueryException
, since it tries to add a foreign key equal to ""
to the pivot table.
On the other hand, I noticed that if the value is null
instead of ""
, the attach()
method is not executed and, thus, does not throws an Exception, neither updates the database, which is exactly the behavior I want, when the user made no selection in the input form.
My question is how can I change the request values from ""
to null
? My first thought was to make a helper function that iterates over the request input array and replace "" with null and use it in the controller BUT it does not seems like a good idea for me, because this would not prevent possible unwanted validation errors, when using Form Request Validation.
Any ideas please ???
Thank you in advance
if ($value !== '0' && empty($value)) {
– Solmization