Laravel 引用外键数据
Laravel Referencing Foreign Key Data
菜鸟问题来了..
$data = DB::table('fquotes')->orderBy('created_at', 'DESC')->get();
我有这个查询,它正在从 fquotes 中提取所有数据。每个项目都有一个 cid,它链接到另一个 table 中的客户 ID。如何包含来自客户 table 的列(名称)与提取的每一行相匹配?
我要显示数据:
客户行中的客户名称 - fquotes 行
在调用将执行查询的 get()
之前,调用 join()
方法,它将通过指定的列将您的 table 加入另一个 table。
$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();
不加入也可以用,只用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"
},
菜鸟问题来了..
$data = DB::table('fquotes')->orderBy('created_at', 'DESC')->get();
我有这个查询,它正在从 fquotes 中提取所有数据。每个项目都有一个 cid,它链接到另一个 table 中的客户 ID。如何包含来自客户 table 的列(名称)与提取的每一行相匹配?
我要显示数据:
客户行中的客户名称 - fquotes 行
在调用将执行查询的 get()
之前,调用 join()
方法,它将通过指定的列将您的 table 加入另一个 table。
$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();
不加入也可以用,只用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"
},