Laravel一对多查询

时间:2018-11-16 21:17:16

标签: postgresql laravel-5 eloquent laravel-query-builder

我有一个表客户端和一个表client_addresses 一个客户可以有很多地址,但是只有一个首选地址

在我的表中,我只需要显示首选地址,或者如果未选择首选地址,则显示插入的最后一个地址,当我在表上搜索时,需要对此记录进行过滤。

示例

client
id  name        
5   Aldo   // 2 address
6   Franco // 4 address
7   Jane   // 0 address
8   Doe    // 1 address

client_addresses
id  address     client_id   preferred
1   1st street  5       true  //Aldo (5) first record inserted but preferred
2   2nd street  5       false
3   3th street  6       false
4   4th street  6       false
5   5th street  6       false
6   6th street  6       false // Franco (6) last address inserted 
7   7th street  8       true  // Doe (8) preferred 

this is my result table (datatable)

client.id   name        address
5       Aldo        1st street
6       Franco      6th street
7       Jane        null
8       Doe         7th street

if i search "1st street", it must show the first record
if i search "2nd,3th,4th,5th street", it should not show any lines
if i search "6th street", it must show the second record
if i search "7th street", it must show the last record

我该如何使用laravel雄辩或查询生成器来做到这一点?

谢谢

0 个答案:

没有答案