3

I have two merged queries and I need to sort (orderBy) by id.

$query1= DB::table('contract')->where('to_user_id',$to_user_id)->get();
$query2= DB::table('contract')->where('from_user_id',$to_user_id)->get();
$query1= $query1->merge($query2);

I tried to orderBy('id') but it ordered individualy not together.

1
  • have a look on this link Commented Jan 29, 2018 at 12:09

2 Answers 2

2

Instead of performing 2 queries and merging them, would it be better to just pull the relevant records in 1 go?

For example:

$query1= DB::table('contract')
        ->where('to_user_id',$to_user_id)
        ->orWhere('from_user_id', $to_user_id)
        ->get();
Sign up to request clarification or add additional context in comments.

Comments

0

You can use sortBy() to sort the collection and values() method to reset the keys to consecutively numbered indexes -

$query1 = $query1->merge($query2);
$query1 = $query1->sortBy('id');

$query1 = $query1->values()->all();

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.