doutui9606 2015-04-28 13:08
浏览 22
已采纳

Laravel引用外键数据

Noob question incoming..

$data = DB::table('fquotes')->orderBy('created_at', 'DESC')->get();

I have this query which is pulling all data from fquotes. Each item has a cid which is linked to a customer id in another table. How do I include a column (name) from the customers table matching each row pulled?

I want to display the data:

customer name from customers row - fquotes row

  • 写回答

2条回答 默认 最新

  • duancan7914 2015-04-28 13:30
    关注

    Before you call get() which will execute the query, call the join() method and it will join your table on the other table by the specified column.

    $data = DB::table('fquotes')
        ->select('customers.id as customer_id', 'fquotes.id as fquotes_id', DB::raw('*'))
        ->orderBy('created_at', 'DESC')
        ->join('customers', 'fquotes.cid', '=', 'customers.id')
        ->get();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 如何通过交互分析得出某高危患者对放疗获益更多
  • ¥15 相关性分析中,p<0.05, r=0.29,怎么评价相关性呢
  • ¥15 docker部署Mongodb后输入命令报错?
  • ¥15 将下列流程图转变成python程序代码
  • ¥15 我需要全国每个城市的最新小区名字等数据。
  • ¥15 开发一个小区生态的小程序
  • ¥15 如何解决Excel中dependent dropdown list 的问题
  • ¥15 MddBootstrapInitialize2失败
  • ¥15 LCD Flicker
  • ¥15 esp32在micropython环境下使用ssl/tls连接mqtt服务器出现以下报错Connected on 192.168.154.223发生意外错误: 5无法连接到 MQTT 代理,如何解决?