首页 文章

Laravel引用外键数据

提问于
浏览
0

Noob问题传入..

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

我有这个查询,它从fquotes中提取所有数据 . 每个项目都有一个cid,它链接到另一个表中的客户ID . 如何在customers表中包含匹配每行拉出的列(名称)?

我想显示数据:

客户名称来自客户行 - fquotes行

2 回答

  • 0

    在调用将执行查询的 get() 之前,请调用 join() 方法,它将通过指定列将您的表连接到另一个表上 .

    $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();
    
  • 0

    您可以在不加入的情况下使用,只需使用foreach即可

    public function index(Request $request, $id)
        {
            $items = ReportGlAssignment::where('shop_id', $id)->paginate(10);
    
            foreach ($items as $item ){
              $bill =  BillingAssignment::where('id',$item->billing_assignment_id )->pluck('assignment');
              $item['billing_assignment_id'] = $bill;
            }
    
            return response()->json($items);
        }
    

    它将显示您的外键数据的名称,

    {
    id: 7,
    designation: "​Food Service",
    income_gl: "333",
    expense_gl: "333",
    billing_assignment_id: "bgjjnhkj",
    shop_id: 12,
    created_at: "2017-08-21 07:26:48",
    updated_at: "2017-08-23 09:35:54"
    },
    

相关问题