2

I have sample code for get all trashed messages current user:

$query1 = $inbox->where('recipient', $user_id)
                ->where('trashed_in_recipient', 1)
                ->where('show_in_recipient', '!=', 1)
                ->orderBy('created_at', 'desc')
                ->paginate($paginate);

$query2 = $inbox->where('sender', $user_id)
                ->where('trashed_in_sender', 1)
                ->where('show_in_sender', '!=', 1)
                ->orderBy('created_at', 'desc')
                ->paginate($paginate);

How can be writed two query in one and get finally result?

Table structure:

enter image description here

6
  • we need context regarding the tables you are refering to. Commented Jan 15, 2018 at 15:05
  • You can see my table structure in updated question! @Thomas Moors Commented Jan 15, 2018 at 15:10
  • Are you using SoftDeletes trait? Commented Jan 15, 2018 at 15:16
  • Yes I use but not now! I will use it if I need to restore messages @Rajender Joshi Commented Jan 15, 2018 at 15:18
  • If you intend to use SoftDeletes, you don’t need trashed_in_recipient and trashed_in_sender columns. Commented Jan 15, 2018 at 15:22

1 Answer 1

3

You can use functions to define WHERE groups:

$query = $inbox->where(function($query) use ($user_id) {
    $query->where('recipient', $user_id);
    $query->where('trashed_in_recipient', 1);
    $query->where('show_in_recipient', '!=', 1);
})->orWhere(function($query) use ($user_id) {
    $query->where('sender', $user_id);
    $query->where('trashed_in_sender', 1);
    $query->where('show_in_sender', '!=', 1);
})->orderBy('created_at', 'desc')
  ->paginate($paginate);

I hope this helps you

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

5 Comments

Thanks @Syncro! And on last of query can I paginate two query results?
inside of callback function using var $user_id take error: Undefined variable: user_id
Change it to function($query) (use $user_id)
Use function($query) use ($user_id) instead of just function($query), sorry :)
Working @Syncro! Generaly +1 for you )

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.