Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

earmsby's avatar

Attach related record with non-standard table name

I'm having difficulty attaching a new existing Account to my Work model using the Filament relationsManager. If I manually link the Work and Account directly in the DB table or create a new Account from the relationManager, everything works as expected. However, when I attempt to attach a new Account from the relationManager, I get an error:

Call to undefined method App\Models\Account::works()

Here is the relationship on the Work model:

public function imprints()
    {
        return $this->belongsToMany(Account::class,'account_work_imprints');
    }

And this is the relationship on the Account model:

public function publishedWorks()
    {
        return $this->belongsToMany(Work::class, 'account_work_imprints');
    }

Note that I cannot name the relationship "works" because I have two different relationships between the Work model and the Account model ("imprints" and "authors").

I suspect that the problem has to do with the fact Filament is expecting the flip side of "imprints" to be "works" instead of "publishedWorks." Is there a method on AttachAction::make() where I can set the correct value?

0 likes
3 replies
LaryAI's avatar
Level 58

You're correct—Filament's relation managers, when handling many-to-many pivot actions (like attach/detach), try to guess the "inverse" relationship method on the related model. By default, it expects the conventional naming: if your main resource has a relation called imprints, it will try to call $account->works() (or similar) on the Account model. When you use non-standard naming, or have multiple relations (like authors and imprints), Filament gets confused unless explicitly configured.

Solution

You need to inform Filament which inverse relationship to use. For this situation, you can use the inverseRelationship property/method on your relation manager.

Example: Specify the Inverse Relationship

In your ImprintsRelationManager, add the following property or override the method:

public static function inverseRelationship(): string
{
    return 'publishedWorks'; // the name of your Account::belongsToMany(Work::class, ...)
}

Or, if you're using an older version of Filament, try:

public static $inverseRelationship = 'publishedWorks';

This tells Filament: "When doing attach/detach actions, use the publishedWorks method on the Account model to look up related works."

Final Notes

  • Your relationships as written are correct.
  • You only need to inform Filament about which method name to use on the related model.
  • You don’t need to change your database schema or relationship definitions.

References:

If you have further customization needs (such as custom pivot data), you can check out the Filament documentation on relation managers for more options.

earmsby's avatar

I just tested changing the relationship to "works" and that solved the problem. On to Larry AI's solution then...

earmsby's avatar

Larry AI's solution may work for Filament 3.0 but I found that simply adding ->inverseRelationship('publishedWorks') to the table method is what did the trick.

Please or to participate in this conversation.