首页 > 解决方案 > 在 laravel 上为 foreach() 提供的参数无效

问题描述

我喜欢错误消息:为 foreach() 提供的参数无效

对于我的方法 index() 我有这个:

public function index(Request $request)
    {   
    if(\Auth::user()->hasRole('admin')){
         if($request->has('search'))
              $remarks = Remark::orderBy('instruction', 'asc')->where('instruction','like','%'.$request->input('search').'%');
         else
             $remarks = Remark::all();
    }else{
         \Auth::user()->load('remarks');
         $remarks = \Auth::user()->remarks;
    }

    return view('admin.remarks.index', compact('remarks'));
    }

关于我的 index.blade.php,我的循环“for”是这样的:

                <th>Instruction</th>
                <th>Description</th>
                <th>Date seance</th>
                <th>Name Candidate</th>
                <th>Email</th>
                </tr>
                </thead>
               @foreach($remarks as $remark)
                <tr>
                   <td> {{$remark->instruction}}</td>
                   <td> {{$remark->description}}</td>
                   <td> {{$remark->seances->date_seance}}</td>
                   <td> {{$remark->seances()->first()->candidates->name}}</td>
                   <td> {{$remark->seances()->first()->candidates->email}}</td>
                   <td>

你知道我的问题吗?我不明白 ??

标签: laravellaravel-5

解决方案


您需要get()在 Eloquent Query Builder 之后调用以从数据库中获取数据。除非它是一个 Eloquent Query Builder 对象(它是不可迭代的,所以不能在 for 循环中使用)。

该函数get()将结果作为 Laravel 集合(可迭代)返回。

$remarks = Remark::orderBy('instruction', 'asc')
               ->where('instruction','like','%'.$request->input('search').'%')
               ->get();

推荐阅读