1. Go to this page and download the library: Download alik-laravel/ordering library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
alik-laravel / ordering example snippets
use Alik\Ordering\HasOrdering;
use Illuminate\Database\Eloquent\Model;
class MenuItem extends Model
{
use HasOrdering;
protected $fillable = ['name', 'order', 'category_id'];
/**
* @return array<string, string>
*/
protected function casts(): array
{
return [
'order' => 'float',
];
}
}
// Models are automatically ordered
$items = MenuItem::all(); // Ordered by 'order' column ASC
// New models get the next order automatically
$newItem = MenuItem::create(['name' => 'New Item']); // order = last_order + 1
// Get an Orderer instance for positioning
$item = MenuItem::find(1);
$orderer = $item->ordering();
// Calculate positions
$beforePosition = $orderer->before(); // Position before current item
$afterPosition = $orderer->after(); // Position after current item
$firstPosition = $orderer->first(); // First position
$lastPosition = $orderer->last(); // Last position
// Reorder all items (fixes gaps in ordering)
MenuItem::reorder();
// Reorder within a scope (e.g., same category)
MenuItem::reorderWithinScope(['category_id' => 1]);
// Get orderer for items within the same category
$orderer = $item->ordering(['category_id' => $item->category_id]);
// Reorder only items with category_id = 1
MenuItem::reorderWithinScope(['category_id' => 1]);
// Handle null values in scope
MenuItem::reorderWithinScope(['parent_id' => null]);
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;
class MenuItemController extends Controller
{
public function updateOrder(Request $request)
{
$validated = $request->validate([
'position' => [' $target = MenuItem::findOrFail($validated['target_id']);
$position = $validated['position'];
// Optional: Add scope validation if needed
// if ($source->category_id !== $target->category_id) {
// return response()->json(['error' => 'Items must be in same category'], 422);
// }
// Update the source item's order based on target position
$source->update([
'order' => $target->ordering()->$position()
]);
return response()->json(['success' => true]);
}
}
public function updateOrder(Request $request)
{
$validated = $request->validate([
'position' => ['integer', 'exists:menu_items,id'],
]);
$source = MenuItem::findOrFail($validated['source_id']);
$target = MenuItem::findOrFail($validated['target_id']);
$position = $validated['position'];
// Ensure items are in the same scope
if ($source->category_id !== $target->category_id) {
return response()->json(['error' => 'Items must be in same category'], 422);
}
// Use scoped ordering
$scopeAttributes = ['category_id' => $target->category_id];
$source->update([
'order' => $target->ordering($scopeAttributes)->$position()
]);
return response()->json(['success' => true]);
}
bash
composer analyse
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.