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
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();
You Can use without joining, just use a 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);
}
It will show the name of your foreign key data,
{
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"
},