Riverworks Logo

October CMS PHP Tinker

October CMSCommand Line

You can set the initial sort_order by editing the orderBy statement according to your data.

https://octobertricks.com/tricks/fix-sort-order-values-existing-set-models

Vendor\YourPlugin\Models\YourModel
    ::orderBy('title') // Change this to modify the inital order
    ->get()
    ->map(function($model, $index) {
        $model->sort_order = $index + 1;
        $model->save();
    });
Console friendly version for pasting after your model query:

->get()->map(function($model, $index) {$model->sort_order = $index + 1;$model->save();});


Example:
RiverworksMarketing\Products\Models\ProductCategory::orderBy('id')->get()->map(function($model, $index) { $model->sort_order = $index + 1; $model->save(); });