laravel - 在 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>
你知道我的问题吗?我不明白 ??
解决方案
您需要get()
在 Eloquent Query Builder 之后调用以从数据库中获取数据。除非它是一个 Eloquent Query Builder 对象(它是不可迭代的,所以不能在 for 循环中使用)。
该函数get()
将结果作为 Laravel 集合(可迭代)返回。
$remarks = Remark::orderBy('instruction', 'asc')
->where('instruction','like','%'.$request->input('search').'%')
->get();
推荐阅读
- python - 仅使用字符串 format() 方法的 Python 格式日期
- python - 从字符串中提取相同匹配的多个实例
- tensorflow - TensorFlow Gradient Tape 的用途是什么?
- java-8 - 如何使用 javax.net.ssl.SSLContext 设置密码套件
- css - 使边框环绕整个窗口会导致显示垂直滚动条?
- java - 如何使用自定义提取 PDF 文本
? - ruby-on-rails - 使用模型名称的别名与 ActiveRecord 建立一对多关联
- python - 如何将记录数组从 arff 文件转换为 ndarray?
- angularjs - UpgradeAdapter.registerForNg1Tests 有什么替代方法吗?
- php - 更新查询运行但不是字段未在 sql 数据库中更新