1

I wish to orderBy (sortBy) two different nested relationship columns in laravel.

I have the following query which returns the data that i need

 return Assignments::with('worker.person', 'order_job.order.company', 'worker.rel_phones', 'worker.rel_emails')
   ->where('assigned', true)
   ->whereNotIn('worker_id', DB::table('files')->select('worker_id')->where([
    ['file_master_type_id', 6],
    ['worker_id', '!=', 0],
   ])
    ->orWhere([
     ['file_master_type_id', 7],
     ['worker_id', '!=', 0],
    ])
    ->whereNotNull('worker_id'))
   ->get();

i would like to be able to order by the company_name column in the order_job.order.company relationship as well as at the same time order by the surname column in the worker.person relationship.

is this possible or would it have to be done using raw sql / joins?

00_adam
  • 11
  • 1
  • 3
  • Not possible with the current approach it can be done via join query, because laravel executes a separate query to eager the related records so main query doesn't have any access to the query which is performed for eager loading – M Khalid Junaid Feb 12 '21 at 20:03
  • 1
    Thanks @MKhalidJunaid I didn't think I was able to do what I was asking however just wanted to check with everyone to see if I would have to use a join query. – 00_adam Feb 13 '21 at 21:13

0 Answers0