URL: http://localhost/?v=
Code:
Route::get('/', ['as' => 'home', function()
{
dd(Request::has('v'));
}]);
Output: false
What is going on? Is this a bug or am I doing something wrong?
Laravel's Illuminate\Http\Request class provides an object-oriented way to interact with the current HTTP request being handled by your application as well as retrieve the input, cookies, and files that were submitted with the request.
The has MethodThe $request->has() method will now return true even if the input value is an empty string or null . A new $request->filled() method has been added that provides the previous behaviour of the has() method.
Request::has()
will check if the item is actually set. An empty string doesn't count here.
What you are looking for instead is: Request::exists()
!
Route::get('/', ['as' => 'home', function()
{
dd(Request::exists('v'));
}]);
Upgrade to Laravel 5.5 or higher. They changed this so now it works as you originally expected.
In the Laravel 5.5 upgrade guide, we read the following:
The
has
Method
The
$request->has()
method will now returntrue
even if the input value is an empty string ornull
. A new$request->filled()
method has been added that provides the previous behaviour of thehas()
method.
The $request->exists()
method still works, it is just an alias for $request->has()
.
$request->exists()
: Determine if the request contains a given input item key.$request->has()
: Determine if the request contains a non-empty value for an input item.$request->exists()
: Alias for $request->has
$request->has()
: Determine if the request contains a given input item key.$request->filled()
: Determine if the request contains a non-empty value for an input item.If you click to the commands above, you can check out the source code and see that they literally just renamed exists
to has
, has
to filled
, then aliased exists
to has
.
Use Request::filled()
because unlike Request::has()
, it also checks if the parameter is not empty.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With