【发布时间】:2018-11-25 01:36:17
【问题描述】:
我想通过在存储用户 ID 的“finished_by”列中找到用户名来打印用户名。但我得到这个错误:
试图获取非对象的属性“名称”
库存模型有finished_by,它有用户ID。
这是我的blade.php
@foreach($inventories as $inventory)
<tr>
<td>{{$inventory['id']}}</td>
<td>{{$inventory->user->name}}</td>
</tr>
@enforeach
和我的索引方法
$company_id = Auth::user()->company_id;
$inventories = Inventory::where('company_id',$company_id)->get();
return view('inventories', compact('inventories', 'companies'));
还有我的关系
Inventory.php
public function users(){
return $this->belongsTo(User::class, 'finished_by');
}
用户.php
public function inventories(){
return $this->hasMany(Inventory::class, 'finished_by');
}
【问题讨论】:
-
更改为
{{$inventory->users->name}} -
没有改变,我有同样的错误。
-
在此处显示您的表架构
标签: laravel error-handling relationships