1

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();
3

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.