Custom sorting on a laravel relationship collection

后端 未结 2 1013
旧巷少年郎
旧巷少年郎 2021-02-14 04:11

I\'m a little stuck on something that usually is quite straight forward. I need to sort records from a hasMany relationship into a custom order based on a certain value and an \

2条回答
  •  滥情空心
    2021-02-14 04:35

    Your sorting will be applied only after you get the result while here $go = $this->hasMany('Product')->orderBy('colour','DESC'); you are just getting an instance of a class which will return can return your result if you apply a get method on it.

    For your purpose you have to use sortBy method of laravel collections after you get the result of your data you can find more (laravel Collection sortBy() method) & here is a similar question answer for you https://stackoverflow.com/a/28202985

提交回复
热议问题