0

For example I have 2 queries:

1:

$q = SomeContent::select('somecontent_id')
    ->where('slug', Request::segment(2))
    ->where('something', $something)
    ->first();

2

$req = SomeContent::select('slug')
    ->where('something', $anothersomething)
    ->where('somecontent_id', $q->somecontent_id)
    ->first();

How would I merge these together as one query if possible in laravel's query builder? I can't find a lot about using selects statements inside where statements.

2 Answers 2

1

You can union them together, something like

// The query builder also provides a quick way to "union" two queries together:

$q = SomeContent::select('somecontent_id')
    ->where('slug', Request::segment(2))
    ->where('something', $something);

$req = SomeContent::select('slug')
    ->where('something', $anothersomething)
    ->where('somecontent_id', $q->somecontent_id)
    ->union($q)->get();
Sign up to request clarification or add additional context in comments.

Comments

1

you could also you the orWhere function

$q = SomeContent::select('somecontent_id')
      ->where('slug', Request::segment(2))
       ->where('something', $something)
        ->orWhere(function($query)
        {
            $query->where('something', $anothersomething)
          ->where('somecontent_id', $q->somecontent_id)
           ->union($q)->get();
        })
        ->get();

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.