2017-07-05 75 views
0

我正在嘗試爲用戶提取所有配置文件數據。並且用戶可以爲服務器上傳多個可以給用戶徽章的文件。我的測試環境中有2個用戶,其中1個用戶有3個文件。現在,當我從控制器返回JSON時,我得到了三個結果,但我想要合併或以某種方式組合,因此一個用戶可以將所有數據放在一個對象中。如何收集Laravel雄辯查詢中的項目?

這是我目前得到恢復

[ 
    { 
     "id":1, 
     "avatar":"john.jpg", 
     "role":2, 
     "first_name":"John", 
     "last_name":"Doe", 
     "user_name":"johndoe", 
     "account_status":2, 
     "one_child":"6", 
     "introduction":"test", 
     "file_type":1, 
     "file":"1498725633.docx", 
     "status":2, 
     "lat":59.44, 
     "lng":24.74, 
     "rating":"5", 
     "recommended":"1", 
     "rating_count":2, 
     "distance":0.29988841983648 
    }, 
    { 
     "id":1, 
     "avatar":"john.jpg", 
     "role":2, 
     "first_name":"Joe", 
     "last_name":"Doe", 
     "user_name":"johndoe", 
     "account_status":2, 
     "one_child":"6", 
     "introduction":"test", 
     "file_type":4, 
     "file":"118771941-merged.mp4", 
     "status":1, 
     "lat":59.44, 
     "lng":24.74, 
     "rating":"5", 
     "recommended":"1", 
     "rating_count":2, 
     "distance":0.29988841983648 
    }, 
    { 
     "id":4, 
     "avatar":"capture.JPG", 
     "role":2, 
     "first_name":"Jane", 
     "last_name":"Doe", 
     "user_name":"janedoe", 
     "account_status":2, 
     "one_child":"4", 
     "introduction":"Test", 
     "file_type":2, 
     "file":"1498732136.docx", 
     "status":1, 
     "lat":59.46, 
     "lng":24.83, 
     "rating":"3", 
     "recommended":"2", 
     "rating_count":2, 
     "distance":5.5651349391591 
    } 
] 

正如你可以看到李四有兩個文件,但我想有一個對象下的所有文件和文件類型,因此將只有一個約翰JSON和一個Jane對象atm。

這裏是我的原始SQL從laravel

select 
    `users`.`id` as `id`, 
    `users`.`avatar`, 
    `users`.`role`, 
    `users`.`first_name`, 
    `users`.`last_name`, 
    `users`.`user_name`, 
    `users`.`account_status`, 
    `user_wage_preferences`.`one_child`, 
    `user_introductions`.`introduction`, 
    `user_files`.`file_type`, 
    `user_files`.`file`, 
    `user_files`.`status` as `status`, 
    `user_contact_informations`.`lat`, 
    `user_contact_informations`.`lng`, 
    ROUND(AVG(user_reviews.rating)) AS rating, 
    SUM(user_reviews.recommended) as recommended, 
    COUNT(user_reviews.id) as rating_count, 
    `user_files`.`status`, 
    (
     6371 * acos(cos(radians(59.4424504)) * cos(radians(lat)) * cos(radians(lng) - radians(24.7377842)) + sin(radians(59.4424504)) * sin(radians(lat))) 
    ) 
    AS distance 
from 
    `users` 
    inner join 
     `user_files` 
     on `users`.`id` = `user_files`.`user_id` 
    left join 
     `user_reviews` 
     on `users`.`id` = `user_reviews`.`nanny_id` 
    inner join 
     `user_introductions` 
     on `users`.`id` = `user_introductions`.`user_id` 
    inner join 
     `user_wage_preferences` 
     on `users`.`id` = `user_wage_preferences`.`user_id` 
    inner join 
     `user_contact_informations` 
     on `users`.`id` = `user_contact_informations`.`user_id` 
where 
    `users`.`role` = ? 
    and `users`.`account_status` = ? 
group by 
    `users`.`id`, 
    `users`.`avatar`, 
    `users`.`role`, 
    `users`.`first_name`, 
    `users`.`last_name`, 
    `users`.`user_name`, 
    `user_files`.`status`, 
    `users`.`id`, 
    `user_contact_informations`.`lat`, 
    `user_contact_informations`.`lng`, 
    `user_wage_preferences`.`one_child`, 
    `user_introductions`.`introduction`, 
    `user_files`.`file_type`, 
    `user_files`.`file` 
having 
    `recommended` > ? 
order by 
    `distance` asc jquery - 3.2.1.min.js:2 Uncaught TypeError: Cannot use 'in' operator to search for 'length' in 
    select 
     `users`.`id` as `id`, 
     `users`.`avatar`, 
     `users`.`role`, 
     `users`.`first_name`, 
     `users`.`last_name`, 
     `users`.`user_name`, 
     `users`.`account_status`, 
     `user_wage_preferences`.`one_child`, 
     `user_introductions`.`introduction`, 
     `user_files`.`file_type`, 
     `user_files`.`file`, 
     `user_files`.`status` as `status`, 
     `user_contact_informations`.`lat`, 
     `user_contact_informations`.`lng`, 
     ROUND(AVG(user_reviews.rating)) AS rating, 
     SUM(user_reviews.recommended) as recommended, 
     COUNT(user_reviews.id) as rating_count, 
     `user_files`.`status`, 
     (
     6371 * acos(cos(radians(59.4424504)) * cos(radians(lat)) * cos(radians(lng) - radians(24.7377842)) + sin(radians(59.4424504)) * sin(radians(lat))) 
    ) 
     AS distance 
    from 
     `users` 
     inner join 
     `user_files` 
     on `users`.`id` = `user_files`.`user_id` 
     left join 
     `user_reviews` 
     on `users`.`id` = `user_reviews`.`nanny_id` 
     inner join 
     `user_introductions` 
     on `users`.`id` = `user_introductions`.`user_id` 
     inner join 
     `user_wage_preferences` 
     on `users`.`id` = `user_wage_preferences`.`user_id` 
     inner join 
     `user_contact_informations` 
     on `users`.`id` = `user_contact_informations`.`user_id` 
    where 
     `users`.`role` = ? 
     and `users`.`account_status` = ? 
    group by 
     `users`.`id`, 
     `users`.`avatar`, 
     `users`.`role`, 
     `users`.`first_name`, 
     `users`.`last_name`, 
     `users`.`user_name`, 
     `user_files`.`status`, 
     `users`.`id`, 
     `user_contact_informations`.`lat`, 
     `user_contact_informations`.`lng`, 
     `user_wage_preferences`.`one_child`, 
     `user_introductions`.`introduction`, 
     `user_files`.`file_type`, 
     `user_files`.`file` 
    having 
     `recommended` > ? 
    order by 
     `distance` asc 

提取我所有的連接,我在遷移做出users table所以user_id引用id。我怎樣才能獲取一個用戶對象下的所有文件?

+0

請向我們展示您用於獲取用戶的laravel代碼 –

回答

3

而不是在查詢中加入usersuser_files表,您可以爲它們添加relation

你的情況給users模式將有一個功能是這樣的:

public function files() 
{ 
    return $this->hasMany('App\UserFiles', 'user_id'); 
    //Where UserFiles is the name of the model of the user_files table 
    //and user_id is the foreign key 
} 

一旦這種關係建立了,你可以通過使用with添加files關係到你的查詢:

Users::with('files')->where(...)->orderBy(...)->get(); 

這將文件加載到用戶對象,像這樣:

{ 
    "id":1, 
    "avatar":"john.jpg", 
    "role":2, 
    "first_name":"John", 
    "last_name":"Doe", 
    "user_name":"johndoe", 
    "account_status":2, 
    "one_child":"6", 
    "introduction":"test", 
    "files": [ 
     { 
      //File Object 
     }, 
     { 
      //File Object 
     } 
    ], 
    "lat":59.44, 
    "lng":24.74, 
    "rating":"5", 
    "recommended":"1", 
    "rating_count":2, 
    "distance":0.29988841983648 
} 
+1

謝謝!我必須重做我的查詢,但我明白你的意思。謝謝 :) – raqulka