Rumah  >  Artikel  >  pembangunan bahagian belakang  >  Eloquent ORM 查询数据的关联扩展

Eloquent ORM 查询数据的关联扩展

WBOY
WBOYasal
2016-06-06 20:36:40890semak imbas

     user [{
         id: 1,
         name: "jack",
         age: "22",
         city_id: 1,
         job_id: 1
     },
     {
         id: 2,
         name: "yoyo",
         age: "22",
         city_id: 2,
         job_id: 2
     }];
     
     city [{
         id: 1,
         name: "beijin"
     },
     {  
        id: 2,
        name: "shanghai"
     }]
     

单独查询表的结果是这样的,我想实现类似下面的扩展结果要怎么操作,看了api文档还是不太明白

    user [{
         id: 1,
         name: "jack",
         age: "22",
         city: {id: 1, name: "beijin"},
         job_id: {id:1, name: "dev"}
     },
     {
         id: 2,
         name: "yoyo",
         age: "22",
         city: {id: 2, name: "shanghai"},
         job: {id: 2, name: "art"}
     }];

回复内容:

     user [{
         id: 1,
         name: "jack",
         age: "22",
         city_id: 1,
         job_id: 1
     },
     {
         id: 2,
         name: "yoyo",
         age: "22",
         city_id: 2,
         job_id: 2
     }];
     
     city [{
         id: 1,
         name: "beijin"
     },
     {  
        id: 2,
        name: "shanghai"
     }]
     

单独查询表的结果是这样的,我想实现类似下面的扩展结果要怎么操作,看了api文档还是不太明白

    user [{
         id: 1,
         name: "jack",
         age: "22",
         city: {id: 1, name: "beijin"},
         job_id: {id:1, name: "dev"}
     },
     {
         id: 2,
         name: "yoyo",
         age: "22",
         city: {id: 2, name: "shanghai"},
         job: {id: 2, name: "art"}
     }];
Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn