saade / filament-adjacency-list

A Filament package to manage adjacency lists (aka trees).
https://filamentphp.com/plugins/saade-adjacency-list
MIT License
74 stars 13 forks source link

[Bug]: Double entries with relationships #37

Open RibesAlexandre opened 15 hours ago

RibesAlexandre commented 15 hours ago

What happened?

Hi,

I've 3 entries in my navigation_items database, a hasMany relationship to Navigation Model like this :

Navigation Model Table :

Capture d’écran 2024-09-28 à 17 00 48

NavigationItem Model Table :

Capture d’écran 2024-09-28 à 17 04 59

My Navigation Model is like this :

class Navigation extends Model
{
    use \Staudenmeir\LaravelAdjacencyList\Eloquent\HasRecursiveRelationships;

    protected $fillable = [
        'name' ,
        'slug' ,
        'position' ,
        //'items'
    ];

    public function items(): HasMany
    {
        return $this->hasMany(NavigationItem::class, 'navigation_id')
            ->whereNull('parent_id')
            ->with('childrenAndSelf')
            ->orderBy('sort');
    }

And this is my NavigationItemModel :

class NavigationItem extends Model
{
    use \Staudenmeir\LaravelAdjacencyList\Eloquent\HasRecursiveRelationships;

    protected $fillable = ['navigation_id', 'parent_id', 'label', 'sort', 'data'];

    public $timestamps = false;

    protected $casts = [
        'data' => 'array'
    ];

    public function children(): HasMany
    {
        return $this->hasMany(NavigationItem::class, 'parent_id')
            ->with('children')
            ->orderBy('sort');
    }

My AdjencyList is like this :

AdjacencyList::make('items')
                            ->label(__('filament-starter::translations.labels.items'))
                            ->visible(fn($record) => $record !== null)
                            ->relationship('items')
                            //->childrenKey('children')
                            ->maxDepth(config('filament-starter.navigation.settings.max_depth', 6))
                            ->form([ // etc...

When I comment ->whereNull('parent_id') from my Navigation Model, items appears twice, like this :

Capture d’écran 2024-09-28 à 17 03 38

But when I keep it, only parents appears :

Capture d’écran 2024-09-28 à 17 04 21

I don't understand if it's a bug or If I do a mistake. I followed the documentation and got this. Any explications ?

Thanks you

How to reproduce the bug

Just create 2 models related with tables :

Navigation

        Schema::create('navigations' , function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('slug')->unique();
            $table->string('position');
            $table->timestamps();
        });
}

// NavigationModel 

    use \Staudenmeir\LaravelAdjacencyList\Eloquent\HasRecursiveRelationships;

    protected $fillable = [
        'name' ,
        'slug' ,
        'position' ,
        //'items'
    ];

    public function items(): HasMany
    {
        return $this->hasMany(NavigationItem::class, 'navigation_id')
            ->whereNull('parent_id')
            ->with('childrenAndSelf')
            ->orderBy('sort');
    }

NavigationItem :

Schema::create('navigation_items' , function (Blueprint $table) {
            $table->id();
            $table->string('label');
            $table->foreignId('navigation_id')->constrained()->references('id')->on('navigations')->cascadeOnDelete();
            $table->foreignId('parent_id')->constrained()->references('id')->on('navigation_items')->cascadeOnDelete();
            $table->json('data');
            $table->string('sort');
        });

// NavigationItemModel 
    use \Staudenmeir\LaravelAdjacencyList\Eloquent\HasRecursiveRelationships;

    protected $fillable = ['navigation_id', 'parent_id', 'label', 'sort', 'data'];

    public $timestamps = false;

    protected $casts = [
        'data' => 'array'
    ];

    public function children(): HasMany
    {
        return $this->hasMany(NavigationItem::class, 'parent_id')
            ->with('children')
            ->orderBy('sort');
    }

And NavigationResource :

AdjacencyList::make('items')
                        ->label(__('filament-starter::translations.labels.items'))
                        ->visible(fn($record) => $record !== null)
                        ->relationship('items')
                        //->childrenKey('children')
                        ->maxDepth(config('filament-starter.navigation.settings.max_depth', 6))
                        ->form([
                            Forms\Components\TextInput::make('label')
                                ->label('Label')
                                ->required(),
                               ->options(['external_link' => 'External Link']),

                            Forms\Components\Group::make()
                                ->statePath('data')
                                ->whenTruthy('type')
                                ->live()
                                ->schema([
                                    Forms\Components\TextInput::make('url')
                                        ->label(__('filament-starter::translations.labels.url'))
                                        ->required(),
                                    Forms\Components\Select::make('target')
                                        ->label(__('filament-starter::translations.navigation.select-options.target'))
                                        ->options([
                                            '' => __('filament-starter::translations.navigation.select-options.same-tab'),
                                            '_blank' => __('filament-starter::translations.navigation.select-options.new-tab'),
                                        ])
                                        ->live()
                                        ->default(''),
                                ]),

Package Version

2.2.1

PHP Version

8.2.0

Laravel Version

^11

Which operating systems does with happen with?

macOS

Notes

No response