【发布时间】:2018-11-26 09:57:00
【问题描述】:
我想在我的刀片视图中访问定义的关系,并在我的发票模型中显示我正在这样做
public function users() {
return $this->hasone('App\Client','id','client_id');
}
在发票控制器中
public function show(Invoice $invoice)
{
$clients = Invoice::with('users')->get();
return view('admin.invoices.show', compact('invoice', $invoice),compact('clients',$clients));
}
最后在我看来我做到了
<td>{{ $clients->users->first()->title }}</td>
但是当我尝试查看时出现此错误
Property [users] does not exist on this collection instance
当我 dd $clients 时,我得到如下关系的结果
#relations: array:1 [▼
"users" => Client {#309 ▼
#fillable: array:14 [▶]
#connection: "mysql"
#table: null
#primaryKey: "id"
#keyType: "int"
+incrementing: true
#with: []
#withCount: []
#perPage: 15
+exists: true
+wasRecentlyCreated: false
#attributes: array:17 [▼
"id" => 1
"title" => "شسی"
【问题讨论】:
-
hasone有错字,正确的是hasOne -
函数名不区分大小写
-
那是我纠正的自动编译,但不是那不是问题仍然得到同样的错误
标签: php laravel view controller laravel-blade