F
Filamentβ€’2mo 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β€’2mo ago
What it not working exactly?
Ermac
ErmacOPβ€’2mo ago
The selected parent is invalid. i get this error from the select field cause this is a non existend parent
CodeWithDennis
CodeWithDennisβ€’2mo ago
Did you setup the relationship in your model? Also Parent is not the right code convention for relationships.
Ermac
ErmacOPβ€’2mo ago
public function Parent() { return $this->belongsTo(ProductCategory::class, 'parent_id'); }
CodeWithDennis
CodeWithDennisβ€’2mo 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
ErmacOPβ€’2mo ago
but dosen't this fail for parent records? the start of the tree has no parent after all
CodeWithDennis
CodeWithDennisβ€’2mo ago
What tree? A select field is not a tree.
Ermac
ErmacOPβ€’2mo ago
mmm i'll try explaining better the table is like this id - name - short description - long description - parent_id - timestamps
CodeWithDennis
CodeWithDennisβ€’2mo 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
ErmacOPβ€’2mo 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β€’2mo ago
Thats where null comes in place. If you want to make a tree, checkout my #codewithdennis-select-tree plugin.
Ermac
ErmacOPβ€’2mo ago
i'll look it up
CodeWithDennis
CodeWithDennisβ€’2mo 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
ErmacOPβ€’2mo 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β€’2mo ago
No point of reinventing the wheel when someone else has put 1000+ hours into it. And you are welcome πŸ‘
Ermac
ErmacOPβ€’2mo ago
it's still complaining i guess i'll have to try your plugin
CodeWithDennis
CodeWithDennisβ€’2mo ago
My plugin is not gonna work if you didn't setup your models properly tho πŸ˜‰
Ermac
ErmacOPβ€’2mo ago
if i quesry the model it works
CodeWithDennis
CodeWithDennisβ€’2mo 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
ErmacOPβ€’2mo ago
at the moment i'm not getting a php error, it's just the select wants a value
Solution
CodeWithDennis
CodeWithDennisβ€’2mo ago
Yea so make it nullable.
Ermac
ErmacOPβ€’2mo ago
ah i feel quite stupid rigth now it worked
CodeWithDennis
CodeWithDennisβ€’2mo ago
πŸ˜‰
Ermac
ErmacOPβ€’2mo ago
i totally forgot the validation part
Want results from more Discord servers?
Add your server