PHP code example of johncarter / filament-nested-sortable
1. Go to this page and download the library: Download johncarter/filament-nested-sortable 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/ */
johncarter / filament-nested-sortable example snippets
use JohnCarter\FilamentNestedSortable\Pages\NestedSortablePage;
class TreeListPages extends NestedSortablePage
{
// ...
}
public static function getPages(): array
{
return [
'index' => Pages\TreeListPages::route('/'),
];
}
public function getCreateRecordFormSchema(): array
{
return [
TextInput::make('title')
->reactive()
->afterStateUpdated(
fn($state, callable $set) =>
$set('slug', Str::slug($state))
),
TextInput::make('slug')
->extraAttributes(['class' => 'font-mono text-gray-500']),
];
}
public function getRecordActions(): array
{
$actions = parent::getRecordActions();
array_unshift($actions, $this->viewAction());
return $actions;
}
public function viewAction(): Action
{
return Action::make('view')
->icon('heroicon-o-eye')
->openUrlInNewTab()
->url(function (array $arguments) {
return $arguments['record']['url'];
});
}
class TreeListPages extends NestedSortablePage
{
public string $recordKeyName = 'uuid'; // Default: 'id'
public string $parentColumn = 'category_id'; // Default: 'parent_id'
public string $orderColumn = 'sort_order'; // Default: 'order'
public string $childrenRelationName = 'subcategories'; // Default: 'children'
protected string $view = 'custom::nested-sortable-page'; // Default: 'filament-nested-sortable::pages.nested-sortable-page'
}
// Customize record fetching
public function getRecords(): EloquentCollection
{
return $this->getResource()::getEloquentQuery()
->with([$this->childrenRelationName, 'author'])
->where('is_published', true)
->orderBy($this->orderColumn)
->get();
}
// Change label column (default: 'title')
public function getRecordLabelColumn(): string
{
return 'name';
}
// Enable/disable clickable records (default: true)
public function hasRecordUrl(): bool
{
return false;
}
// Customize record URLs
public function getRecordUrl($record): string
{
return route('pages.show', $record['slug']);
}
// Customize label rendering
public function getRecordLabel($record): Htmlable | string
{
$label = $record->{$this->getRecordLabelColumn()};
return $this->hasRecordUrl()
? new HtmlString('<a href="' . $this->getRecordUrl($record) . '">' . $label . '</a>')
: $label;
}
// Customize page title
public function getTitle(): string
{
return 'Category Management';
}
// Add/remove actions
public function getRecordActions(): array
{
$actions = parent::getRecordActions();
array_unshift($actions, $this->viewAction());
return $actions;
}
// Override edit action
public function editAction(): Action
{
return Action::make('edit')
->icon('heroicon-o-pencil')
->color('warning')
->url(fn($arguments) => $this->getResource()::getUrl('edit', ['record' => $arguments['record'][$this->recordKeyName]]));
}
// Override delete action
public function deleteAction(): Action
{
return Action::make('delete')
->label('Remove')
->color('danger')
->
// Customize header actions
public function getHeaderActions(): array
{
return [
Action::make('create')
->label('Add New Category')
->schema($this->getCreateRecordFormSchema())
->action(fn($data) => $this->createRecord($data)),
Action::make('import')
->url(route('categories.import')),
];
}
// Customize create form
public function getCreateRecordFormSchema(): array
{
return [
TextInput::make('name')->),
];
}
// Customize creation logic
public function createRecord(array $data): void
{
$data['order'] = $this->getNextOrder();
$this->getResource()::getModel()::create($data);
$this->records = $this->getRecords();
}
// Customize update logic
public function persistRecordUpdates($pendingRecordUpdates)
{
foreach ($pendingRecordUpdates as $update) {
$record = $this->getResource()::getModel()::find($update[$this->recordKeyName]);
$record->update($update);
}
$this->records = $this->getRecords();
$this->dispatch('reset-pending-record-updates');
}
css
@import '/vendor/johncarter/filament-nested-sortable/resources/views/**/*.blade.php';