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

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 51单片机中C语言怎么做到下面类似的功能的函数(相关搜索:c语言)
  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程