LaravelPHP

How to Sort Collections in Laravel and PHP

You can use sortBy for laravel collection for sorting by custom orders.

You can do like this:

PHP

Also, you could add an attribute accessor to your model which does the same.

PHP
PHP

If you need this sort of functionality in multiple places I suggest you create your own Collection class.

PHP

Then, to actually use that class override newCollection() in your model. In this case it would be in the ChecklistItems class:

PHP

I hope it will help you, feel free to comment if you have any question.

Shaiv Roy

Hy Myself shaiv roy, I am a passionate blogger and love to share ideas among people, I am having good experience with laravel, vue js, react, flutter and doing website and app development work from last 7 years.

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button