Laravel - Requests - Determining If Input Is Present
You may use the has
method to determine if a value is present on the request. The has
method returns true
if the value is present on the request:
if ($request->has('name')) {
//
}
When given an array, the has
method will determine if all of the specified values are present:
if ($request->has(['name', 'email'])) {
//
}
The whenHas
method will execute the given closure if a value is present on the request:
$request->whenHas('name', function ($input) {
//
});
A second closure may be passed to the whenHas
method that will be executed if the specified value is not present on the request:
$request->whenHas('name', function ($input) {
// The "name" value is present...
}, function () {
// The "name" value is not present...
});
The hasAny
method returns true
if any of the specified values are present:
if ($request->hasAny(['name', 'email'])) {
//
}
If you would like to determine if a value is present on the request and is not empty, you may use the filled
method:
if ($request->filled('name')) {
//
}
The whenFilled
method will execute the given closure if a value is present on the request and is not empty:
$request->whenFilled('name', function ($input) {
//
});
A second closure may be passed to the whenFilled
method that will be executed if the specified value is not "filled":
$request->whenFilled('name', function ($input) {
// The "name" value is filled...
}, function () {
// The "name" value is not filled...
});
To determine if a given key is absent from the request, you may use the missing
method:
if ($request->missing('name')) {
//
}