Home > OS >  get ids from join in laravel
get ids from join in laravel

Time:11-17

I am getting data from join in laravel but i cant get rides id using get or pluck method in query.


$datas = \App\Models\Order::find($id); $new = explode(',',$id);


$orderss = DB::table('orders')
    ->join('jobs', 'jobs.order_id', '=', 'orders.id')
    ->join('rides', 'rides.job_id', '=', 'jobs.id')
    ->whereIn('orders.id', $new)
    ->get();

here is the data enter image description here

i want to get data of rides.id from query whenever i use get or pluck method i got error

`

$datas = \App\Models\Order::find($id);
$new = explode(',',$id);


$orderss = DB::table('orders')
    ->join('jobs', 'jobs.order_id', '=', 'orders.id')
    ->join('rides', 'rides.job_id', '=', 'jobs.id')
    ->whereIn('orders.id', $new)
    ->get('id');

`

enter image description here

CodePudding user response:

In the second image you posted error show that 'id' field is ambiguous which means there are multiple columns named 'id' and it is true. There are three 'id' columns coming from orders table, jobs table and rides table. So you need to tell that which 'id' column you want to extract. For this purpose you can try following code.

$orderss = DB::table('orders')
->join('jobs', 'jobs.order_id', '=', 'orders.id')
->join('rides', 'rides.job_id', '=', 'jobs.id')
->whereIn('orders.id', $new)
->get('rides.id')

OR

$orderss = DB::table('orders')
->join('jobs', 'jobs.order_id', '=', 'orders.id')
->join('rides', 'rides.job_id', '=', 'jobs.id')
->whereIn('orders.id', $new)
->select('rides.id')
->get()

these both should work and return you the collection of rides ids.

  • Related