on your order model try doing this
class Order extends Eloquent{
public function customer()
{
return $this->hasOne(Customer::class)->orderBy('customer_name', 'asc');
}
}
Thank you for your answer!
No, this didn't work. Its being printed out as before
Hi @znittzel, Try ordering in the with closure.
$order = Order::with(['customer'=>function($q){
$q->orderBy('customer_name');
}]);
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?
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
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community