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

  • please post the code so we can try to help you fix it – richyen Dec 22 '19 at 06:18
  • This needs more details. Please describe the table, column type and any code you are using to display the data. – ryantxr Dec 22 '19 at 06:22
  • Show your `controller` where you have written the `method`.. For PHP you can use `natsort($array);`. – Chonchol Mahmud Dec 22 '19 at 06: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);` – Israr ul Hassan Dec 23 '19 at 07:23
  • https://stackoverflow.com/a/36716875/2943403 – mickmackusa Aug 22 '22 at 08:11

1 Answers1

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');
Jon White
  • 682
  • 4
  • 12