Reorganise product categories by dragging, in Filament
A wholesaler of packaging and fasteners has grown its range. Tape used to be one of the packaging categories; now there are glues, sealants and tape, and they belong together under a new "Adhesives" group. Labels and Boxes are always ordered together, so the buyer wants them as one category.
In most admin panels that means opening each category's edit form and changing a "Parent" select, then fixing the products of a deleted category by hand, one by one. Nobody can see the whole tree while doing it, and nobody knows afterwards who moved what.
This article does the same with Warp Tree: the whole tree on one page, a new category added under Packaging, Tape dragged into it, and Labels merged into Boxes with its products. All pictures are from the live demo.
The scene: the tree on one page
The categories page shows the tree. Groups have a folder icon, the categories with products under them a tag. The badges count what is under each item: "4 items" under a group, "3 products" in a category.
Step 1: a tree page for the resource
The categories table only needs a parent_id column. The page is a TreePage, and it replaces the resource's list page. This is the demo's page, without its "View code" button:
use Filament\Support\Icons\Heroicon;
use Qalainau\FilamentWarpTree\Pages\TreePage;
use Qalainau\FilamentWarpTree\Support\TreeRecords;
class CategoryTree extends TreePage
{
protected static string $resource = CategoryResource::class;
// "Export CSV" and "Import CSV" above the tree
protected static bool $hasCsvActions = true;
public function getTreeIcon(Model $record, bool $hasChildren): string|BackedEnum|Htmlable|null
{
return $hasChildren ? Heroicon::OutlinedFolder : Heroicon::OutlinedTag;
}
// The products filed under each category: counted in the tree,
// and moved along when a category is merged or deleted
public function getTreeRecords(): array
{
return [TreeRecords::make(Product::class, 'category_id')->label('product|products')->canBeEmpty()];
}
}
// CategoryResource
public static function getPages(): array
{
return [
'index' => CategoryTree::route('/'),
];
}
Run php artisan migrate once for the history table. Your own tables are not changed.
Step 2: add "Adhesives" under Packaging
Hover over Packaging and press +. The new item goes under it.
Step 3: drag Tape into it
Grab the handle on the left of Tape and drop it in the middle of Adhesives. Dropping on the upper or lower edge of an item puts it before or after it instead.
The keyboard works too: Alt + → puts an item under the one above it, Alt + ↑ ↓ moves it among its siblings.
Step 4: merge Labels into Boxes
Merging is not a drag: it moves the products and the items under Labels to Boxes, then deletes Labels. The window says exactly that before anything happens. The target is chosen from a tree select, so it also works when the target is far away in a large tree.
Deleting works the same way: it asks what happens to the items under it and where the products go.
Step 5: who changed what
History lists the changes, newest first, with who made them and when.
How it stays correct
- Someone else moved it first. If another person moved Tape after your page was opened, your move is refused and the branches are read again. The page never keeps showing a stale tree.
- No loops. An item can't be moved or merged into its own branch.
- Permissions come from the resource: adding needs
canCreate(), moving and renamingcanEdit(), merging and deletingcanDelete(). - One transaction per change: if a model event stops a save, nothing is changed and a notice says so.
Other scenes
- Departments, with the employees moved along when two departments are merged.
- A chart of accounts or document folders.
- A menu of three levels:
protected static ?int $maxDepth = 3;refuses anything deeper, while dragging and again on the server. - People who see only their branch: a regional manager edits only the stores under their region.
The same package also has the pieces around the tree: a SelectTree field to pick a category in the product form, the path Packaging › Adhesives › Tape in tables, and a filter for "this branch and everything under it".
Limits worth knowing
- Without an order column (
getTreeOrderColumn()), items are listed by name, and dragging only changes the parent. Add an order column to let people set the order. - A depth limit added later does not move items that are already deeper; they can be moved up, not further down.
Try it
Open the categories demo, open Packaging and drag Tape somewhere else. Then merge two categories and look at History. No sign-up, and the data is reset every hour.