F
Filament14mo ago
DevMike

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:
Fieldset::make()
->relationship('user_details', ['field'=>'company']),
->schema([
TextInput::make('value')
])
Fieldset::make()
->relationship('user_details', ['field'=>'company']),
->schema([
TextInput::make('value')
])
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.php
How 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
Dennis Koch
Dennis Koch14mo ago
Please read #✅┊rules for formatting and error sharing
DevMike
DevMikeOP14mo ago
Sorry for this dump question: Where is the exception page to click on "share" for error codes? Never heard of...
Dennis Koch
Dennis Koch14mo ago
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.
DevMike
DevMikeOP14mo ago
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.
public function user_details()
{
return $this->hasMany('App\Models\UserDetails', 'user_id', 'id');
}
public function user_details()
{
return $this->hasMany('App\Models\UserDetails', 'user_id', 'id');
}
Got my solution adding this to the model "user":
public function user_detail_company()
{
return $this->hasOne('App\Models\UserDetails', 'user_id', 'id')->where('field', 'company');
}
public function user_detail_company()
{
return $this->hasOne('App\Models\UserDetails', 'user_id', 'id')->where('field', 'company');
}
and setting relationship of the form element to this new relation:
Fieldset::make()
->schema([
TextInput::make('value')
])
->relationship('user_detail_company'),
Fieldset::make()
->schema([
TextInput::make('value')
])
->relationship('user_detail_company'),
Maybe not the optimal way to make this - but working and so fine for now. 😉
Want results from more Discord servers?
Add your server