0

I have this query:

User::leftJoin('friends', function ($join) {
    $join->on('friends.user_id_1', '=', 'users.id')
        ->orOn('friends.user_id_2', '=', 'users.id');
})
->where(function ($query) use ($myID) {
    // Group orwhere functions so the query builder knows these belong together
    $query->where([
        'friends.user_id_1' => $myID,
        'friends.accepted' => true
    ])
    ->orWhere([
        'friends.user_id_2' => $myID,
        'friends.accepted' => true
    ]);
})
->where('users.id', '!=', $myID) // Exclude the user with id $myID
->get();

https://stackoverflow.com/a/41832867/5437864

I want to use this query twice, but with a different where clause. Is it possible to reuse this query without copying the whole code? And if so, how?

Community
  • 1
  • 1
Z0q
  • 1,689
  • 3
  • 28
  • 57

2 Answers2

1

I used the PHP clone keyword. I'm not sure if this is the best solution, but it helps. Any other suggestions are welcome.

$friends_query = User::leftJoin('friends', function ($join) {
    $join->on('friends.user_id_1', '=', 'users.id')
        ->orOn('friends.user_id_2', '=', 'users.id');
})
->where(function ($query) use ($myID) {
    // Group orwhere functions so the query builder knows these belong together
    $query->where([
        'friends.user_id_1' => $myID,
        'friends.accepted' => true
    ])
    ->orWhere([
        'friends.user_id_2' => $myID,
        'friends.accepted' => true
    ]);
});

$friends_me = clone $friends_query;
$friends_me = $friends_me->where('users.id', '!=', $myID);

$friends_others = clone $friends_query;
$friends_others = $friends_others->where('users.id', '=', $myID);
Z0q
  • 1,689
  • 3
  • 28
  • 57
0
$query = User::leftJoin('friends', function ($join) {
    $join->on('friends.user_id_1', '=', 'users.id')
        ->orOn('friends.user_id_2', '=', 'users.id');
})
->where(function ($query) use ($myID) {
    // Group orwhere functions so the query builder knows these belong together
    $query->where([
        'friends.user_id_1' => $myID,
        'friends.accepted' => true
    ])
    ->orWhere([
        'friends.user_id_2' => $myID,
        'friends.accepted' => true
    ]);
});

Adding extra where to existing query

$query->where('users.id', '!=', $myID)


$query->get();

Check this link for another example

Example

Community
  • 1
  • 1
Rashad
  • 1,344
  • 2
  • 17
  • 33
  • It's not working. The other link is using query builder. – Z0q Jan 25 '17 at 08:35
  • It does not give any errors, but it applies 2 filters to the same query instead of one filter per copy. I used the `clone` keyword (see my answer). – Z0q Jan 25 '17 at 08:44
  • I don't understand you, actually, what do you mean. But, you did same thing, with more long way. There is not any 2 filters. You can every time add some conditions to the main `$query` which I provided. – Rashad Jan 25 '17 at 08:49
  • I'm sorry, but this solution does not give the expected result. Otherwise I would have upvoted it. – Z0q Jan 25 '17 at 08:50
  • May be it is not expected result for you, but you haven't provided more information, what do you want to do, actually. Your question wasn't so understandable. – Rashad Jan 25 '17 at 08:52