Is it possible to use isEmpty()
method for validating a input a for empty field? I have a validate($request)
where it has a multiple form to be filled up. I have additional form where it's not totally required.
Controller:
$this->validate($request,
[
'title' => 'required|regex:/(^[A-Za-z0-9 ]+$)+/|max:255',
'content' => 'required',
'category' => 'required',
'approver' => 'required',
'recipient' => 'required',
]);
$document = new Document();
$user = Auth::user();
//Request in the form
$document->title = $request->title;
$document->content = $request->content;
$document->category_id = $request->category;
$document->save();
foreach($request->recipient as $recipientId)
{
$document->sentToUsers()->sync([ $recipientId => ['sender_id' => $user->id]],false );
}
I have a form name department
which is not required to be filled out. I'm trying to check if it's not empty it will insert this into database. If empty nothing will happen.
if(!$request->department->isEmpty())
{
foreach($request->department as $departmentId)
{
foreach(Department::find($departmentId)->users()->get() as $user1) //find the users belonging to the current department
{
$document->sentToUsers()->sync([ $user1->id => ['sender_id' => $user->id]],false );
}
}
}
else
{
}
You can always use empty()
:
if(!empty($request->department))
I guess isEmpty()
is for collection only and you're checking property of Request
object:
The isEmpty method returns true if the collection is empty; otherwise, false is returned
Doing $request->field
will give you the value of that field, be it a string, array, number or anything else. You can just use PHP's empty
function to check if it's empty before doing something:
if (!empty($request->department)) {
// ...
}
As noted by other answers the isEmpty
method you're referring to is for collections whereas you're checking property values.
However, I wanted to add one very import note about the use of empty
in that stead.
In PHP the empty
construct is semantically identical to !isset($thing) || !$thing
such that both empty($thing)
and !isset($thing) || !$thing
give identical results. The isset
construct is simply used to determine if a variable is not null
without triggering any use of undefined variable errors. Combine that with a boolean truth check, and we get empty
.
This might not always be what you're after though. For example, in PHP an object is always true in the truth table. However, things like the integer 0
, and empty string ""
, or even an empty Array
are considered false in the truth table. Which is why it's always important to check the truth table before you make your decision about which to use.
So for example, a zero-length string, may be empty to you, but it may not be empty to me if the expectation is that the user submitted the value isset($_POST['thing']) === true
, but as an empty string.
You should also consider things like <input type="checkbox">
where the expectation is that the browser will not send the value at all if it is unchecked. Or the fact that over HTTP, all values are initially strings. So you may want to use filters like filter_var("false", FILTER_VALIDATE_BOOLEAN)
which turns the string "false"
(that wouldn't evaluate to empty
) into the boolean value false
(that would).