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
25 Replies
What it not working exactly?
The selected parent is invalid.
i get this error from the select field
cause this is a non existend parent
Did you setup the relationship in your model?
Also
Parent
is not the right code convention for relationships.public function Parent() {
return $this->belongsTo(ProductCategory::class, 'parent_id');
}
Well first of all use lowercase π
but dosen't this fail
for parent records?
the start of the tree has no parent after all
What tree?
A select field is not a tree.
mmm i'll try explaining better
the table is like this id - name - short description - long description - parent_id - timestamps
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.
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 );
Thats where
null
comes in place.
If you want to make a tree, checkout my #codewithdennis-select-tree plugin.i'll look it up
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
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
No point of reinventing the wheel when someone else has put 1000+ hours into it.
And you are welcome π
it's still complaining i guess i'll have to try your plugin
My plugin is not gonna work if you didn't setup your models properly tho π
if i quesry the model it works
Just a little tip: When you need help, always share your code in the right format and include any errors you're getting.
at the moment i'm not getting a php error, it's just the select wants a value
Solution
Yea so make it nullable.
ah i feel quite stupid rigth now
it worked
π
i totally forgot the validation part