PHP code example of saade / filament-adjacency-list
1. Go to this page and download the library: Download saade/filament-adjacency-list 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/ */
saade / filament-adjacency-list example snippets
use Saade\FilamentAdjacencyList\Forms\Components\AdjacencyList;
AdjacencyList::make('subjects')
->form([
Forms\Components\TextInput::make('label')
->
AdjacencyList::make('subjects')
->labelKey('name') // defaults to 'label'
AdjacencyList::make('subjects')
->childrenKey('children') // defaults to 'children'
AdjacencyList::make('subjects')
->maxDepth(2) // defaults to -1 (unlimited depth)
AdjacencyList::make('subjects')
->itemAction('edit') // or view, delete, moveUp, indent etc ...
->itemUrl(
fn (array $item) => YourResource::getUrl('view', ['record' => $item['id']]) // for example
)
// App/Models/Department.php
class Department extends Model
{
public function subjects(): HasMany
{
return $this->hasMany(Subject::class)->whereNull('parent_id')->with('children')->orderBy('sort');
}
}
// App/Models/Subject.php
class Subject extends Model
{
protected $fillable ['parent_id', 'name', 'sort']; // or whatever your columns are
public function children(): HasMany
{
return $this->hasMany(Subject::class, 'parent_id')->with('children')->orderBy('sort');
}
}
// App/Filament/Resources/DepartmentResource.php
AdjacencyList::make('subjects')
->relationship('subjects') // Define the relationship
->labelKey('name') // Customize the label key to your model's column
->childrenKey('children') // Customize the children key to the relationship's method name
->form([ // Define the form
Forms\Components\TextInput::make('name')
->label(__('Name'))
->
// App/Models/Department.php
class Department extends Model
{
use \Staudenmeir\LaravelAdjacencyList\Eloquent\HasRecursiveRelationships;
public function getPathSeparator()
{
return '.children.';
}
}
class Department extends Model
{
use \Staudenmeir\LaravelAdjacencyList\Eloquent\HasRecursiveRelationships;
public function getCustomPaths()
{
return [
[
'name' => 'tree_path',
'column' => 'id',
'separator' => '.children.',
],
];
}
}
AdjacencyList::make('subdepartments')
->relationship('descendants') // or 'descendantsAndSelf', 'children' ...
->customPath('tree_path') // if you're using custom paths
AdjacencyList::make('subdepartments')
->orderColumn('sort') // or any other column
class DepartmentTreeWidget extends AdjacencyListWidget
{
protected static string $relationshipName = 'descendantsAndSelf';
// If you're using a widget on an Edit or View page, the plugin will automatically set the $record for you.
// However, you're free to override this method to customize the record.
public function getModel(): Model | string | null
{
return Department::query()->where(['is_root' => true])->first();
}
// You can configure the widget using the same methods as the form component.
protected function adjacencyList(AdjacencyList $adjacencyList): AdjacencyList
{
return $adjacencyList
->label('Foo')
->editable()
->labelKey('nombre')
->childrenKey('hijos');
}
}