【发布时间】:2021-07-02 04:10:25
【问题描述】:
我正在使用 Laravel 8 开发我的项目,在这个项目中,我有一个名为 hostings 的表,它的迁移在这里:
public function up()
{
Schema::create('hostings', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}
现在我想从数据库返回一些结果并在刀片上显示它们,所以我添加了这个:
<table>
<tr>
<th>Hosting Name:</th>
<td>{{ $hosting->name }}</td>
</tr>
</table>
但现在我得到了这个错误:
Property [name] does not exist on this collection instance
那么这里出了什么问题?我该如何解决这个问题?
如果您与我分享任何想法或建议,我将不胜感激,
提前致谢。
控制器代码:
public function index()
{
$hosting = Hosting::all();
return view('admin.index', compact('hosting'));
}
【问题讨论】:
-
请分享控制器代码
-
@OMR 抱歉,我刚刚添加了它
-
Hosting::all返回用于循环遍历每个项目的数组。 -
collection instance这意味着你有很多行需要循环以获得单个值 -
如果托管中只有一个值,则使用“first”而不是“all”