Form field in relationship to pivot table
Hello, just one question with edititing a resource of admin panel:
I have a common user model with id as primary field and name, language etc.
Ther is a pivot subtable user_details with a hasMany relationship from users -> hasMany(user_details) identified by user_id.
user-details has just 4 columns: id, user_id, field, value.
Task: I want to add to the user-form single rows of user_details as seperated fixed form-fields.
Until now I have added a relationshipManager to user_details giving me a table with every entry according to user_id, what is nice so far.
But I need to build a custom user-form where I can place each possible field of "user_details" identified by the value of column field as the name of the formfield itself.
My approach:
I am having a fieldset with relationship to user_details, trying to display the field 'value'
In my form of UserResource.php:
But it gives me just an TypeError:
Filament\Forms\Components\Fieldset::relationship(): Argument #2 ($condition) must be of type Closure|bool, array given, called in /var/www/html/app/Filament/Resources/UserResource.phpHow do I correctly connect the Fieldset with the related row of user_details where 'field' = 'nameOfField' ? Is that possible? Thanks for any advice for I am new to filament and little used to laravel. PHP 8.1.23 Laravel 9.52.10 Filament v2.17.50 (project is not updateable for me)
4 Replies
Please read #✅┊rules for formatting and error sharing
Sorry for this dump question: Where is the exception page to click on "share" for error codes?
Never heard of...
That’s the screen you get when you have an error
Just read your message. The error is clear enough. You cannot provide an array as second argument to relationship method. The fields need to be defined in the schema and you need a Pivot model on your relationship.
Thank you for your answere! I know, that I can't pass an array to relationship. Just wanted to demonstrate what I trying to do there...
Do you have an example for me for this pivot model, please? Meanwhile I am searching online...
Thought "hasMany" is a pivot model relationship.
Got my solution adding this to the model "user":
and setting relationship of the form element to this new relation:
Maybe not the optimal way to make this - but working and so fine for now. 😉