2016-08-04 37 views
1

我們使用belongsToMany()在我們的用戶模型中建模了自我引用關係。用戶可以是代理商或賣家彼此 - 所以我們定義seller()agents()在Laravel中獲取關係後獲取查詢構建器對象

現在我們使用https://github.com/Nayjest/Grids需要查詢到網格。

在我們想要的網格中顯示賣家的代理商。

目前我們正在使用手工查詢 - 但我們想從模型中提取邏輯。

因此,我們需要的是它取

$seller = Auth::user(); // or any other instance of User 
$seller->agents 

我們試圖

$query = $seller->newQuery()->where('laravel_reserved_1.id', '=', $seller->id); 
return $user->agents()->getRelationQuery($query, $query); 

沒有運氣時執行的查詢(如查詢生成器對象)。

編輯

試圖$出賣人>代理商() - > getQuery(),我們得到

SQLSTATE[23000]: Integrity constraint violation: 1052 Column 'id' in order clause is ambiguous 
(SQL: select * from `users` inner join `user_connections` on 
`users`.`id` = `user_connections`.`user_id` 
where `user_connections`.`related_user_id` = 2 and   
`user_connections`.`type` = agent order by `id` asc 
limit 15 offset 0) 
+0

什麼是變量'$ seller'? – TheFallen

+0

@TheFallen A用戶實例 – Alex

+0

是不是隻是'$ user-> agents() - > getQuery()'? – alepeino

回答

0

原來這只是

$user->agents()->getQuery() 

我們有一欄「 ID「在網格中導致在過程中稍後的模糊查詢。