Reputation: 1476
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
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