1

I am using laravel 6.x, i have rows like this

Plot 4
Plot 5
Plot 2
Plot 4B
Plot 3
Shop 2
Shop 1

I want result like this

Plot 2
Plot 3
Plot 4
Plot 4B
Plot 5
Shop 1
Shop 2

Thanks

5
  • please post the code so we can try to help you fix it
    – richyen
    Dec 22, 2019 at 6:18
  • This needs more details. Please describe the table, column type and any code you are using to display the data.
    – ryantxr
    Dec 22, 2019 at 6:22
  • Show your controller where you have written the method.. For PHP you can use natsort($array);. Dec 22, 2019 at 6:26
  • I have accounts table having column type varchar, \App\Account::whereHas('parent', function($q){ $q->where('title', 'Sales'); })->with('item', 'journals', 'item.sales', 'item.sales.reminders', 'item.sales.user') ->orderByRaw('LENGTH(title)') ->orderBy('title') ->paginate(30); Dec 23, 2019 at 7:23

1 Answer 1

1

If you're using Laravel you should look at their documentation for collections and their sort method.

Start with creating collections: https://laravel.com/docs/6.x/collections#creating-collections

And then look at sorting: https://laravel.com/docs/6.x/collections#method-sort

That should get you there. If you post details of your attempts so far we can help you get it working.

If you're using an eloquent model to get your data from a database then it will come as a collection already then you simply need to sort by the relevant key eg.

Model::all()->sortBy('column');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.