以最短的方式检查 Laravel Blade 中是否有任何请求输入

tar*_*nik 0 laravel laravel-blade

我想检查我的 URL 中的数据是否有任何参数?我以前用过这个条件

@if(app('request')->input('type') || app('request')->input('type-2') || app('request')->input('type-3') || ... many more....)
   <-- code here --> 
@endif
Run Code Online (Sandbox Code Playgroud)

现在它的参数太多了。有没有最短的方法来检查是否有任何参数?喜欢app('request')->has('input')这种最短路线吗?请帮忙注意:laravel 版本 6.19

Dan*_*Dan 5

Request::hasAny()应该可以解决你的问题:

@if (request()->hasAny('type', 'type-2', 'type-3'))
Run Code Online (Sandbox Code Playgroud)

它检查当前请求中是否存在任何给定的输入项键。您还可以用来Request::has检查是否所有给定的输入项键都存在。


要安全地检查当前请求中是否有任何输入,请使用Request::except()

@if (!empty(request()->except('_token', '_method')))
Run Code Online (Sandbox Code Playgroud)

  • 那么你可以使用 `!empty(request()-&gt; except('_token', '_method'))`。这将安全地检查请求是否具有任何属性。 (2认同)