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条)

报告相同问题?