Php Larvel 8:此集合实例上不存在属性[name]
我正在与Laravel 8合作开发我的项目,在这个项目中,我有一个名为Php Larvel 8:此集合实例上不存在属性[name],php,laravel,laravel-8,Php,Laravel,Laravel 8,我正在与Laravel 8合作开发我的项目,在这个项目中,我有一个名为hostings的表,它的迁移如下: public function up() { Schema::create('hostings', function (Blueprint $table) { $table->id(); $table->string('name'); $table->timestamps();
hostings
的表,它的迁移如下:
public function up()
{
Schema::create('hostings', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}
现在我想从DB返回一些结果并在blade上显示,所以我添加了以下内容:
<table>
<tr>
<th>Hosting Name:</th>
<td>{{ $hosting->name }}</td>
</tr>
</table>
那么这里出了什么问题?如何解决此问题
如果您能与我分享任何关于这方面的想法或建议,我将不胜感激
提前谢谢
控制器代码:
public function index()
{
$hosting = Hosting::all();
return view('admin.index', compact('hosting'));
}
试试这个:
<table>
<tr>
@foreach($hosting as $h)
<th>Hosting Name:</th>
<td>{{ $h->name }}</td>
@endforeach
</tr>
@foreach($h作为主机)
主机名:
{{$h->name}
@endforeach
当您拥有集合时,您需要在其上循环以获取值
@foreach($host作为$host托管)
主机名:
{{$host->name}
@endforeach
否则,您可以用逗号显示所有主机名,并用逗号分隔
主机名:
{{$hosting->pull('name')->join(',')}
或者,您可以像这样获取收集的第一个数据
主机名:
{{$hosting->first()->name}
参考号
链接
请共享控制器code@OMR抱歉,我刚刚添加了它
Hosting::all
返回循环遍历每个项目时必须使用的数组。collection instance
这意味着您有太多行需要循环才能获得单个值如果宿主中有单个值,请使用“first”而不是“all”
<table>
<tr>
@foreach($hosting as $h)
<th>Hosting Name:</th>
<td>{{ $h->name }}</td>
@endforeach
</tr>