It seems like you're trying to automatically set an order value for a new record in your database, but you're not using a specific package like spatie/eloquent-sortable to handle the ordering logic. If you want to implement this functionality manually, you can do so by setting the order_by value before saving the model.
Here's a basic example of how you might set the order_by value when creating a new record:
use App\Models\YourModel;
// ...
public function store(Request $request)
{
// Create a new instance of the model
$newRecord = new YourModel($request->all());
// Set the order_by value to the max order_by value plus one
$newRecord->order_by = YourModel::max('order_by') + 1;
// Save the new record
$newRecord->save();
// ...
}
In this example, before saving the new record, we retrieve the maximum order_by value from the existing records using YourModel::max('order_by') and then add 1 to it. This ensures that the new record will have the next highest order number.
If you want to handle reordering when records are deleted or when you want to change the order of existing records, you'll need to implement additional logic to update the order_by values accordingly.
Remember to replace YourModel with the actual model name you are using in your application. Also, ensure that you have proper error handling in place in case there are no existing records to avoid trying to increment a null value.