0

Is it possible to combine the following two queries into one?

$session_id = DB::table('oauth_access_tokens')->where('id', '=', $access_token)->value('session_id');
$owner_id = DB::table('oauth_sessions')->where('id', '=', $session_id)->value('owner_id');

Thanks for your help!

1
  • @JosephSilber - From the first query I get the session_id value and then I use this in the second query to be able to get the owner_id. I was hoping it would be possible to have the first query as a part of the second one. The tables do not have the same columns. Commented Sep 1, 2015 at 13:31

1 Answer 1

1

What you need is a subquery:

$query = DB::table('oauth_sessions');

$query->where('oauth_sessions.id', function ($query) use ($access_token) {
    $query->from('oauth_access_tokens')
          ->where('oauth_access_tokens.id', $access_token)
          ->select('session_id');
});

$owner_id = $query->value('owner_id');

You can chain it all instead of using 3 separate statements. I just formatted it because I think it reads nicer this way.

Sign up to request clarification or add additional context in comments.

1 Comment

Great! Works perfectly. Thank you the help.

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.