本文介紹了Laravel Eloquent:如何僅從連接表中獲取某些列的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
我在 Eloquent 中有 2 個連接表,即主題和用戶.
I have got 2 joined tables in Eloquent namely themes and users.
主題模型:
public function user() {
return $this->belongs_to('User');
}
用戶模型:
public function themes() {
return $this->has_many('Theme');
}
我的 Eloquent api 調用如下所示:
My Eloquent api call looks as below:
return Response::eloquent(Theme::with('user')->get());
返回主題中的所有列(很好)和用戶的所有列(不好).我只需要用戶模型中的用戶名"列,如何將查詢限制為該列?
Which returns all columns from theme (that's fine), and all columns from user (not fine). I only need the 'username' column from the user model, how can I limit the query to that?
推薦答案
更改模型以指定要選擇的列:
Change your model to specify what columns you want selected:
public function user() {
return $this->belongs_to('User')->select(array('id', 'username'));
}
并且不要忘記包括您要加入的專欄.
And don't forget to include the column you're joining on.
這篇關于Laravel Eloquent:如何僅從連接表中獲取某些列的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!