[英]reciveing 2 records from a hasOne relation laravel 5.6
这是我设定关系的模型
public function user() {
return $this->hasOne('App\Client','id','client_id');
}
在这里,控制器有两个关系在这里,我的发票产品定位效果很好,但是$ clients显示了两行
$clients = Invoice::with('user')->get();
$invoice_id = $invoice->id;
$invoices = Invoice::with('products')->where('id', '=', $invoice_id)->firstOrFail();
return view('admin.invoices.show', compact('invoice','invoices'),compact('clients'));
和视图
@foreach($clients as $client)
<td>{{ $client->user->title ?? 'بدون مشتری' }}</td>
@endforeach
现在,当我访问视图时,我得到2个带有相同客户端名称(标题)值的td标签,知道我做错了什么吗? 这是
#observables: []
#relations: array:1 [▼
"user" => Client {#769 ▼
#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" => "مشتری اول"
"description" => "مشتری اول"
"fax" => 123
"adrress1" => "مشتری اول"
"adrress2" => null
"adrress3" => null
"adrress4" => null
"adrress5" => null
"telephone1" => 123
"telephone2" => null
"telephone3" => null
"telephone4" => null
"telephone5" => null
"client_type" => null
"created_at" => null
"updated_at" => null
问题是我正在遍历该模型可能发生的所有关系,所以我只是做了一个简单的过滤器,该过滤器是我的关系的are子句,并设置了发票的ID以仅携带我需要的属性和那个如我在文档中所见,效率更高
$clients = Invoice::with('user')->where('id','=',$invoice_id)->get();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.