F
Filamentβ€’15mo ago
Daniel Plomp

AttachAction doesn't work

I have a simple one-to-many relation between Clients and Contacts. I want to select existing contacts for a Client from the RelationManager. If I use the AttachAction from the Header Actions I get an error, which is available here: https://flareapp.io/share/lm2DjkL7#F70 Call to undefined method Illuminate\Database\Eloquent\Relations\HasMany::getRelatedKeyName() Not sure what I am doing wrong?
Flare
Call to undefined method Illuminate\Database\Eloquent\Relations\HasMany::getRelatedKeyName() - The error occurred at https://careerguide.test:8443/admin/clients/46/edit
14 Replies
Dennis Koch
Dennis Kochβ€’15mo ago
Your relation should be a BelongsToMany
Daniel Plomp
Daniel Plompβ€’15mo ago
Hmm... It is One Client that Has Many Contacts? Not sure I understand. This is in my Client model:
public function contacts()
{
return $this->hasMany(Contact::class);
}
public function contacts()
{
return $this->hasMany(Contact::class);
}
And this is in my Contact model:
public function client()
{
return $this->belongsTo(Client::class);
}
public function client()
{
return $this->belongsTo(Client::class);
}
Dan Harrin
Dan Harrinβ€’15mo ago
you need AssociateAction its in the docs
Daniel Plomp
Daniel Plompβ€’15mo ago
I tried. It gives me the same error
Dan Harrin
Dan Harrinβ€’15mo ago
Dan Harrin
Dan Harrinβ€’15mo ago
please send that error then
Daniel Plomp
Daniel Plompβ€’15mo ago
Sorry, not entirely the same: https://flareapp.io/share/47qrk2E5#F75
Flare
Call to undefined method App\Models\Contact::clients() - The error occurred at https://careerguide.test:8443/admin/clients/46/edit
Dan Harrin
Dan Harrinβ€’15mo ago
protected static ?string $inverseRelationship = 'client';
Daniel Plomp
Daniel Plompβ€’15mo ago
Is that a workaround, or something that I need anyways? I just try to understand what I am doing wrong. The HasMany should be the right relationship type, right?
Dennis Koch
Dennis Kochβ€’15mo ago
It something you need if the guessed relationship doesn't match
Daniel Plomp
Daniel Plompβ€’15mo ago
OK. I'll try it.
Dennis Koch
Dennis Kochβ€’15mo ago
I still think it's a BelongsToMany, but I might be wrong πŸ˜…
Daniel Plomp
Daniel Plompβ€’15mo ago
I see you point now. Maybe you are right. If it is just one-to-many the CreateAction would be sufficient. With a Many-To-Many you would need the AttachAction I guess. I don't have a pivot table. Sorry, I led you astray following a completely different relationship type πŸ˜…
Dan Harrin
Dan Harrinβ€’15mo ago
you have a one-to-many AssociateAction is for when you have contacts with client_id=null and want to associate them to the current client