One to Many Relationship. Implementing --associate

''' Hi, I have, Tables: businesses briefcases So, a briefcase can have many business. Business Model:
public function briefcase(): belongsTo
{
return $this->belongsTo(Briefcase::class);
}
public function briefcase(): belongsTo
{
return $this->belongsTo(Briefcase::class);
}
Briefcase Model:
public function businesses(): hasMany
{
return $this->hasMany(Business::class);
}
public function businesses(): hasMany
{
return $this->hasMany(Business::class);
}
Relation Manager --associate:
php artisan make:filament-relation-manager BriefcaseResource businesses name --view --associate
php artisan make:filament-relation-manager BriefcaseResource businesses name --view --associate
When trying to associate a business I get:
Call to undefined method App\Models\Business::briefcases()
Call to undefined method App\Models\Business::briefcases()
Any suggestions? Thank you.
9 Replies
Dennis Koch
Dennis Koch2y ago
I think you need to define $inverseRelationship = 'business'; in this case
Pablo Torres
Pablo TorresOP2y ago
Hi Dennis, Thank you for time and advice. Your suggestion doesn't appear to solve my issue... Following Laravel naming conventions I named the inverse of the relationship:
public function businesses(): hasMany
{
return $this->hasMany(Business::class);
}
public function businesses(): hasMany
{
return $this->hasMany(Business::class);
}
I have renamed it to:
public function business(): hasMany
{
return $this->hasMany(Business::class);
}
public function business(): hasMany
{
return $this->hasMany(Business::class);
}
I have also regenerated...
php artisan make:filament-relation-manager BriefcaseResource business name --view --associate
php artisan make:filament-relation-manager BriefcaseResource business name --view --associate
No luck...
Dennis Koch
Dennis Koch2y ago
I am sorry. I meant briefcase and it belongs on the relation manager class
Pablo Torres
Pablo TorresOP2y ago
Hi, I think I don't get what you mean... You suggest I have to generate my BrifcaseResource relation manager as follows:
sail artisan make:filament-relation-manager BriefcaseResource briefcase name --associate
sail artisan make:filament-relation-manager BriefcaseResource briefcase name --associate
Thanks.
Dennis Koch
Dennis Koch2y ago
No, no need to generate something new. Just edit the relation manager file and add the property
Pablo Torres
Pablo TorresOP2y ago
Hey, Thank you! For the record... on my BriefcaseResource, RelationManagers, BusinessesRelationManager I added:
protected static ?string $inverseRelationship = 'briefcase';
protected static ?string $inverseRelationship = 'briefcase';
In the docs I found that $inverseRelationship should be used when "relationships with unconventional naming conventions". I don't see this is my case... If you have a minute, is briefcase/briefcases somehow not supported?
Dennis Koch
Dennis Koch2y ago
And that still doesn’t work?
Pablo Torres
Pablo TorresOP2y ago
Now It is works...! I should say thank you, now it works.
Dennis Koch
Dennis Koch2y ago
Nice. The default for RM is a belongsToMany/hasMany in both directions. That's why you need to add the inverseRelationship. Filament doesn't know about it from your models
Want results from more Discord servers?
Add your server