【问题标题】:How to check if Laravel request has no input如何检查 Laravel 请求是否没有输入
【发布时间】:2014-10-28 14:39:27
【问题描述】:

我只想在我的request 不包含任何inputs 时显示文本

目前,产品列表显示在以下页面上:/products。在同一页面上,有一个包含多个输入的表单来过滤产品。

如果我按价格过滤,我会在提交 GET 表单后获得以下 URL:/products?price=true

我正在使用以下条件来检查请求是否有价格输入:if(Input::has('price'))

这很好用,但我还有其他几个请求参数,我想仅在没有填充这些输入时显示文本。

这是我目前所拥有的,它有效,但并不理想:

@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

有没有办法以更简单的方式做类似的事情?

【问题讨论】:

    标签: php laravel validation request


    【解决方案1】:
    @if (!request()->filled('price') || !request()->filled('mixed') || !request()->filled('male') || !request()->filled('female'))
      // Display my text here
    @endif
    
    • 在 Laravel 5.4 之前:

      • $request->exists:判断请求是否包含给定的输入项键。

      • $request->has:判断请求是否包含输入项的非空值。

    • 在 Laravel 5.5 中:

      • $request->exists: $request->has 的别名
      • $request->has:判断请求是否包含给定的输入项键。
      • $request->filled:确定请求是否包含输入项的非空值。

    【讨论】:

    • 嗨,皮马尔。当我使用 $request->filled('fcm_token') 和 fcm_token 在 $request 中不存在时会发生什么。
    • @AssadYaqoob 它将返回false。如果您的输入并不总是出现在表单中,您可以改用whenFilledlaravel.com/docs/8.x/requests#determining-if-input-is-present
    • 非常感谢
    【解决方案2】:

    获取所有输入数据,并统计:

    @if ( ! count(Input::all()))
      // Display text here
    @endif
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-27
      • 2021-06-25
      • 1970-01-01
      • 1970-01-01
      • 2023-03-24
      • 2016-07-30
      • 2018-08-30
      相关资源
      最近更新 更多