你好,我想知道我做错了什么,我得到这个错误是我的控制器和视图
$invoice_specs = invoice::find($id);
$view=view('admin.invoices.pdf',compact('invoice_specs',$invoice_specs));
这就是我的看法
<table class="table ">
<thead>
<tr>
<th>id</th>
<th>name</th>
<th>description</th>
</tr>
</thead>
<tbody>
@foreach($invoice_specs as $invoice_spec)
<tr>
<td>{{$invoice_spec->id}}</td>
<td>{{$invoice_spec->title}}</td>
<td>{{$invoice_spec->description}}</td>
</tr>
@endforeach
</tbody>
</table>
最后是当我dd $ invoice_specs
#attributes: array:5 [▼
"id" => 3
"title" => "سومین فاکتور"
"description" => "سومین فاکتور"
"created_at" => "2018-06-12 21:31:57"
"updated_at" => "2018-06-12 21:31:57"]
#original: array:5 [▼
"id" => 3
"title" => "سومین فاکتور"
"description" => "سومین فاکتور"
"created_at" => "2018-06-12 21:31:57"
"updated_at" => "2018-06-12 21:31:57"
]
问题在于,您调用的用于从数据库检索数据的静态查找方法仅返回单个模型,而不返回集合。因此,当您在视图中尝试将foreach与变量一起使用$invoice_specs
时,实际上并不会遍历模型集合。
在Laravel关于Eloquent和检索单个模型的文档中,您可以看到find
和first
方法仅返回单个模型,您不能在foreach循环中使用该模型。
Laravel文档的这一部分说明了如何检索模型以及如何使用它们。我建议你先阅读:)
因此,当前,如果要显示单个模型,可以做的是摆脱foreach
视图中的循环,在控制器中,应将变量重命名invoice_specs
为invoice_spec
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句