为foreach()提供的参数无效laravel5.8

时间:2019-06-05 14:11:36

标签: laravel-5.8

我想向用户显示他们的级别分别为git checkout 86b403574c90f16b77da4bef8d67a71471ad7f77 admin

operator

但是我得到这个错误

  

为foreach()提供的参数无效(视图:C:\ xampp \ htdocs \ new \ shopping \ resources \ views \ Admin \ levelAdmin \ create.blade.php)

1 个答案:

答案 0 :(得分:0)

我不知道您是否正在使用(\App\User::whereLevel('admin')->get() || \App\User::whereLevel('operator')->get(),因为其中一个查询可能为空,或者是否要获取level匹配adminoperator的用户

对于第一种情况,请尝试以下操作:

@if(!empty(\App\User::whereLevel('admin')->get()))
    @foreach(\App\User::whereLevel('admin')->get() as $user)
        //
    @endforeach
@else
    @foreach(\App\User::whereLevel('operator')->get()  as $user)
        //
    @endforeach
@endif

如果要获得leveloperatoradmin匹配的用户,则应使用somenthin修改查询,如下所示:

@foreach(\App\User::where('level', 'admin')->orWhere('level', 'operator')->get() as $user)
    //
@endforeach