laravel 即使存在参数,Request::has() 也会返回 false

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29218662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 16:56:11  来源:igfitidea点击:

Request::has() returns false even when parameter is present

laravellaravel-5

提问by Bald

URL: http://localhost/?v=

网址: http://localhost/?v=

Code:

代码:

Route::get('/', ['as' => 'home', function()
{
    dd(Request::has('v'));
}]);

Output: false

输出: false

What is going on? Is this a bug or am I doing something wrong?

到底是怎么回事?这是一个错误还是我做错了什么?

回答by lukasgeiter

Request::has()will check if the item is actually set. An empty string doesn't count here.

Request::has()将检查该项目是否实际设置。空字符串在这里不算数。

What you are looking for instead is: Request::exists()!

您正在寻找的是:Request::exists()

Route::get('/', ['as' => 'home', function()
{
    dd(Request::exists('v'));
}]);

回答by totymedli

tl;dr

tl;博士

Upgrade to Laravel 5.5 or higher.They changed this so now it works as you originally expected.

升级到 Laravel 5.5 或更高版本。他们改变了这一点,所以现在它可以像你最初预期的那样工作。

Explanation

解释

In the Laravel 5.5 upgrade guide, we read the following:

Laravel 5.5 升级指南中,我们阅读了以下内容:

The hasMethod

The $request->hasmethod will now return trueeven if the input value is an empty string or null. A new $request->filledmethod has been added that provides the previous behavior of the hasmethod.

has方法

$request->has方法现在将返回true即使输入值是一个空字符串或 null$request->filled添加了一个新方法,该has方法提供了该方法的先前行为。

The $request->existsmethod still works, it is just an alias for $request->has.

$request->exists方法仍然有效,它只是一个别名$request->has

Examining the source code

检查源代码

  • In Laravel 5.4:
    • $request->exists: Determine if the request contains a given input item key.
    • $request->has: Determine if the request contains a non-empty valuefor an input item.
  • In Laravel 5.5:

If you click to the commands above, you can check out the source code and see that they literally just renamed existsto has, hasto filled, then aliased existsto has.

如果您单击上面的命令,您可以查看源代码并看到它们实际上只是重命名existshashasfilled,然后别名existshas

回答by Delino

You might wanna check this out. since the $request->has()method and it property can offer access to request origin.

你可能想看看这个。因为$request->has()方法和 it 属性可以提供对请求来源的访问。

It's ok to use $request->has('username')This will check if <input type="text" name="username" />username attributes actually exist or the params/.query string actually have that key on the request global.

可以使用$request->has('username')这将检查<input type="text" name="username" />用户名属性是否实际存在或 params/.query 字符串实际上在请求全局上具有该键。

回答by doncadavona

Use Request::filled()because unlike Request::has(), it also checks if the parameter is not empty.

使用Request::filled()因为与 不同Request::has(),它还检查参数是否为空。

回答by Limon Monte

As to me it's not a bug, but feature :) In your example vis provided, but it's empty.

对我而言,这不是错误,而是功能 :) 在您的示例v中提供了,但它是空的。

In framework codeyou'll find this:

框架代码中,您会发现:

if ($this->isEmptyString($value)) return false;

So, if empty string is provided has()method will return false. It makes sense to me, in most cases I want this behavior.

因此,如果提供空字符串,has()方法将返回false. 这对我来说很有意义,在大多数情况下我想要这种行为。