F
Filamentβ€’4w ago
Ermac

Hi, i need help handling a self referencing table via Select

hi, i'm trying to create a select for a self referencing table, but i seem to have run on some issues, is there a preferred way to do it? looking around most samples i saw did not work if table was empty, or the foreign key had a default value for level 0 of the tree, i wrote it like this: Components\Select::make('parent_id')->relationship('Parent', titleAttribute: 'name' )->default(0), but the selct field says it's invalid
Solution:
Yea so make it nullable.
Jump to solution
25 Replies
CodeWithDennis
CodeWithDennisβ€’4w ago
What it not working exactly?
Ermac
Ermacβ€’4w ago
The selected parent is invalid. i get this error from the select field cause this is a non existend parent
CodeWithDennis
CodeWithDennisβ€’4w ago
Did you setup the relationship in your model? Also Parent is not the right code convention for relationships.
Ermac
Ermacβ€’4w ago
public function Parent() { return $this->belongsTo(ProductCategory::class, 'parent_id'); }
CodeWithDennis
CodeWithDennisβ€’4w ago
Well first of all use lowercase πŸ˜›
Forms\Components\Select::make('parent_id')
->relationship('parent', 'name')
->required(),
Forms\Components\Select::make('parent_id')
->relationship('parent', 'name')
->required(),
Ermac
Ermacβ€’4w ago
but dosen't this fail for parent records? the start of the tree has no parent after all
CodeWithDennis
CodeWithDennisβ€’4w ago
What tree? A select field is not a tree.
Ermac
Ermacβ€’4w ago
mmm i'll try explaining better the table is like this id - name - short description - long description - parent_id - timestamps
CodeWithDennis
CodeWithDennisβ€’4w ago
Its not that difficult tho, a category can belong to a category and you have a parent_id (nullable) that refers to its own table.
Ermac
Ermacβ€’4w ago
ahh maybe it's cause my parent_id filed was not nullable but had a default gonna change that and retry i put a default cause i though i could serch for all the parents doing where('parent_id' = 0 );
CodeWithDennis
CodeWithDennisβ€’4w ago
Thats where null comes in place. If you want to make a tree, checkout my #codewithdennis-select-tree plugin.
Ermac
Ermacβ€’4w ago
i'll look it up
CodeWithDennis
CodeWithDennisβ€’4w ago
I recommend this package for recursive models; https://github.com/staudenmeir/laravel-adjacency-list
GitHub
GitHub - staudenmeir/laravel-adjacency-list: Recursive Laravel Eloq...
Recursive Laravel Eloquent relationships with CTEs - staudenmeir/laravel-adjacency-list
Ermac
Ermacβ€’4w ago
i'm still lerning so i was trying not too use too many packages since laravel ->livewire -> filament are already quite a bit @CodeWithDennis thank you for helping me
CodeWithDennis
CodeWithDennisβ€’4w ago
No point of reinventing the wheel when someone else has put 1000+ hours into it. And you are welcome πŸ‘
Ermac
Ermacβ€’4w ago
it's still complaining i guess i'll have to try your plugin
CodeWithDennis
CodeWithDennisβ€’4w ago
My plugin is not gonna work if you didn't setup your models properly tho πŸ˜‰
Ermac
Ermacβ€’4w ago
if i quesry the model it works
CodeWithDennis
CodeWithDennisβ€’4w ago
Just a little tip: When you need help, always share your code in the right format and include any errors you're getting.
Ermac
Ermacβ€’4w ago
at the moment i'm not getting a php error, it's just the select wants a value
Solution
CodeWithDennis
CodeWithDennisβ€’4w ago
Yea so make it nullable.
Ermac
Ermacβ€’4w ago
ah i feel quite stupid rigth now it worked
CodeWithDennis
CodeWithDennisβ€’4w ago
πŸ˜‰
Ermac
Ermacβ€’4w ago
i totally forgot the validation part
Want results from more Discord servers?
Add your server