user3720435
user3720435

Reputation: 1476

Laravel query where subquery

I am looking for the best way to perform the query below as a Laravel 4.2 query. I am struggling on the where clause which has a sub query to work.

What my query is doing: I have a parent table with a 1-many child table. I want to get a list of all the child records associated to the parent record only knowing a current child record.

select child.id, child.jobNumber
from child
where child.parentId = (select child.parentId from child where child.id = 2)
order by child.jobNumber, child.id

Something like:

$query = DB::table('child');
$query->select(array('child.id','child.jobNumber'));
$query->orderBy('child.jobNumber');
$query->orderBy('child.id');

// how do I do my where clause?

$list = $query->get();

Upvotes: 1

Views: 1399

Answers (1)

rand0mb1ts
rand0mb1ts

Reputation: 229

What about using whereRaw? http://laravel.com/docs/4.2/queries#advanced-wheres

$query = DB::table('child');
$query->select(array('child.id','child.jobNumber'));
$query->whereRaw('child.parentId = (select child.parentId from child where child.id = 2)');
$query->orderBy('child.jobNumber');
$query->orderBy('child.id');

// how do I do my where clause?

$list = $query->get();

Upvotes: 1

Related Questions