donglian5309 2017-04-26 12:45
浏览 477
已采纳

Laravel:合并两个表中的数据

I have a users table which have a relation with professions, skills, designations, locations etc. The simple user information i can get with this command app\users::find(1) and its skill information like this app\user::find(1)->skill. How can i get all information like general, profession, skills about a specific user with a single command?

For Example:

localhost:8000/users/1

This link should give me json result like below:

{
  "id": 0,
  "name": "string",
  "email": "string",
  "gender": "string",
  "age": 0,
  "mobile_number": "string",
  "company_name": "string",
  "verification_status": 0,
  "image_url": "string",
  "joining_date": "2017-04-26T12:34:34.501Z",
  "message": "string",
  "profession": {
    "id": 0,
    "name": "string"
  },
  "designation": {
    "id": 0,
    "name": "string"
  },
  "location": {
    "id": 0,
    "longitude": 0,
    "latitude": 0,
    "address": "string",
    "city": "string",
    "country": "string"
  },
  "interests": [
    {
      "id": 0,
      "name": "string",
      "custom": "string"
    }
  ],
  "skills": [
    {
      "id": 0,
      "name": "string",
      "custom": "string"
    }
  ]
}
  • 写回答

1条回答 默认 最新

  • duangao8359 2017-04-26 12:48
    关注

    You can load all relations of that model using eager loading and then serialize that object to a json string.

    Like so:

    app\user::where('id', 1)->with(['profession', 'designation', '...'])->first()->toJson();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀