Support the ongoing development of Laravel.io →
Database Eloquent
Last updated 2 years ago.
0

on your order model try doing this

class Order extends Eloquent{

    public function customer()
    {
        return $this->hasOne(Customer::class)->orderBy('customer_name', 'asc');
    }

}
Last updated 8 years ago.
0

Thank you for your answer!

No, this didn't work. Its being printed out as before

0

Hi @znittzel, Try ordering in the with closure.

    $order = Order::with(['customer'=>function($q){
        $q->orderBy('customer_name');
    }]);
0

Thank you for your answer! Though that didn't work either. I still get them ordered by $order->id. I'm doing

 $order = Order::with(['customer'=>function($q){
        $q->orderBy('customer_name');
    }])->get();

but that'll make no difference, right?

0

what you want is for the whole collection to be ordered by the customer_name, right? i did something similar but it didn't feel too right. i post it as a question to see if anybody can come up with a better solution. u can look up the code at the question here: http://laravel.io/forum/01-14-2016-is-there-a-way-to-sort-whole-collection-based-on-related-model

0

Sign in to participate in this thread!

Eventy

Your banner here too?

znittzel znittzel Joined 24 Oct 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2024 Laravel.io - All rights reserved.