sort_order) for ordering. If absent, the package will not auto-migrate (must be added manually).1, 2, 3). Non-sequential gaps (e.g., 1, 3, 5) may cause performance issues with ORDER BY.ORDER BY.deleted_at is present, sorting may include soft-deleted records unless filtered).ORDER BY is not indexed. Requires manual index creation:
ALTER TABLE posts ADD INDEX sort_order_idx (ordering);
sortable column values (e.g., negative numbers, non-numeric data).BRIN index).Laravel/Lumen Core:
sortable trait in controllers).Model::query()->orderBy(...) may override sorting).ORDER BY (e.g., global scopes, filters).Database:
ORDER BY behavior with large datasets.ORDER BY).ordering column (or custom name) to the target table:
Schema::table('posts', function (Blueprint $table) {
$table->integer('ordering')->unsigned()->default(0);
});
CREATE INDEX idx_posts_ordering ON posts(ordering);
Sortable trait to the model and configure the column:
class Post extends Model
{
use Sortable;
protected $sortable = 'ordering'; // Optional if using default
}
// routes/api.php
Route::put('/posts/reorder', [PostController::class, 'reorder']);
// PostController.php
public function reorder(Request $request) {
$order = $request->input('order'); // e.g., [3, 1, 2]
Post::reorder($order);
return response()->json(['success' => true]);
}
PUT requests to a reorder endpoint.{ "order": [3, 1, 2] }
Post::sorted()->paginate(10)).ordering values.ORDER BY.EXPLAIN ANALYZE on ORDER BY).ordering index.ordering values are corrupted.version column) or database transactions for reordering.$sortable property matches the DB column.EXPLAIN).DB::enableQueryLog() to inspect generated SQL.tinker:
$post = Post::find(1);
$post->moveToTop(); // Verify behavior
How can I help you explore Laravel packages today?