【问题标题】:How to collect items in Laravel eloquent query?如何在 Laravel 雄辩查询中收集项目?
【发布时间】:2017-12-09 06:03:57
【问题描述】:

我正在尝试获取用户的所有个人资料数据。并且用户可以为服务器上传多个文件,这可以给用户徽章。我的测试环境中有 2 个用户,其中 1 个用户有 3 个文件。现在,当我从控制器返回 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_idusers table 中引用id。如何获取一个用户对象下的所有文件?

【问题讨论】:

  • 请告诉我们你用来获取用户的 laravel 代码

标签: php mysql json laravel


【解决方案1】:

您可以向它们添加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
}

【讨论】:

  • 谢谢你!我必须重做我的查询,但我明白你的意思。谢谢:)
猜你喜欢
  • 2013-11-29
  • 2017-11-11
  • 2018-01-26
  • 2021-07-23
  • 2023-04-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多