Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sort json object laravel

Tags:

json

php

laravel

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.

like image 657
nesh Avatar asked Jul 04 '26 12:07

nesh


1 Answers

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();
like image 113
Darren Avatar answered Jul 06 '26 00:07

Darren