如何检查 Laravel 请求是否没有输入
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25655443/
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
How to check if Laravel request has no input
提问by PhilMarc
I am using Laravel 4.
我正在使用 Laravel 4。
I am willing to display a text, only if my request contains no input
我愿意显示文本,仅当我的请求不包含 input
For instance, I have a product list mysite/products
and I have an input field to filter by price, which gives the following URL once clicked: mysite/products?price=true
例如,我有一个产品列表mysite/products
,我有一个按价格过滤的输入字段,单击后会显示以下 URL:mysite/products?price=true
I can use if(Input::has('price'))
to check if I have this input, but the thing is that I have a bunch of input fields, and I would like to display text only when there are no inputs.
我可以if(Input::has('price'))
用来检查我是否有这个输入,但问题是我有一堆输入字段,我只想在没有输入时显示文本。
I am thinking about something like:
我正在考虑类似的事情:
@if(Input::has('price')||Input::has('mixed')||Input::has('male')||Input::has('female')||Input::has('kid')||Input::has('page'))
//Do nothing
@else
//Display my text here
@endif
But in a simplified way...
但以一种简化的方式......
I was thinking of checking the URi with Request::segment(1)
and see if it matches 'products'
but it does even with the inputs active.
我正在考虑检查 URIRequest::segment(1)
并查看它是否匹配,'products'
但即使输入处于活动状态也可以。
采纳答案by PhilMarc
@if (!request()->filled('price') || !request()->filled('mixed') || !request()->filled('male') || !request()->filled('female'))
// Display my text here
@endif
Before 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 value for an input item.
In Laravel 5.5:
$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.
在 Laravel 5.4 之前:
$request->exists
:确定请求是否包含给定的输入项键。$request->has
:确定请求是否包含输入项的非空值。
在 Laravel 5.5 中:
$request->exists
: $request->has 的别名$request->has
:确定请求是否包含给定的输入项键。$request->filled
:确定请求是否包含输入项的非空值。
回答by Joseph Silber
Get all input data, and count it:
获取所有输入数据,并对其进行计数:
@if ( ! count(Input::all()))
// Display text here
@endif