php - 如何在 Laravel Eloquent 查询中收集项目?

标签 php mysql json laravel

我正在尝试获取用户的所有配置文件数据。并且用户可以为服务器上传多个文件,这可以给用户徽章。我的测试环境中有 2 个用户,其中 1 个用户有 3 个文件。现在,当我从 Controller 返回 JSON 时,我得到三个结果,但我想合并或以某种方式合并,以便一个用户可以将所有数据放在一个对象中。

这是我目前返回的 JSON

[
    {
        "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
    }
]

如您所见,john doe 有两个文件,但我希望所有文件和文件类型都在一个对象下,这样就只有一个 John 和一个 Jane 对象 atm。

这是我从 laravel 中提取的原始 sql

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 

我在迁移中建立了所有连接,因此 user_id 引用了 users 表 中的 id。如何获取一个用户对象下的所有文件?

最佳答案

您可以添加 relation,而不是在查询中加入 usersuser_files 表给他们。

在您的情况下,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
}

一旦你建立了这个关系,你就可以使用 withfiles 关系添加到你的查询中:

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
}

关于php - 如何在 Laravel Eloquent 查询中收集项目?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44928437/

相关文章:

PHP curl 似乎正在剥离帖子主体

javascript - 根据数据库中的数据进行多项选择

mysql - 持久性与非持久性——我应该使用哪个?

ruby-on-rails - 强制所有操作的渲染格式

php - 对继承对象重新排序/移动 Silverstripe 3 CMS 选项卡

php - 具有 "template"功能和 JQuery 和 PHP 视觉帮助的 Dreamweaver 的替代品

MySql:按id和parent排序记录

php - SaaS方式访问多个数据库和master数据库的用户认证机制

python - 在不反序列化的情况下操作 JSON

javascript - IP 到国家/地区、城市、纬度、语言 javascript 服务