有关 Laravel hasAny() 的文档

问题描述 投票:0回答:2

有人看过 Laravel 5.5 文档如何在请求上使用 hasAny() 方法吗?

php laravel laravel-5
2个回答
0
投票

您将一组键传递给 MessageBag 以查看其中是否存在任何键:

$keys = ['email', 'password'];

if($errors->hasAny($keys)) {
    //if either email, password, or both exist it will hit this conditional
}

0
投票

该请求不提供任何

hasAny()
方法,因为请求输入是
Symfpny\Component\ParameterBag
实例,并且它不提供您正在寻找的方法。
hasAny()
存在于
Illuminate\Support\MessageBag
中,但无法从请求本身检索。要检查请求输入是否具有任何请求的键,您必须迭代所有请求参数并使用
has()
方法执行检查。

你可以用一个衬垫来做到这一点,有点原始,但它有效:

if(count(array_intersect(['foo', 'bar'], $request->keys()) > 0) {
    // do your stuff
}

或者你必须循环所有“任何”参数:

$any = ['foo', 'bar'];

$check = false;
foreach($any as $item) {

   if($request->has($item)) $check = true;
}

if($check) {
   // do your stuff
}
© www.soinside.com 2019 - 2024. All rights reserved.