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

skoobi's avatar
Level 13

Filament relationships on the same table

Morning. I'm trying (un-successfully) to have a relationship with the same Model. My idea and thought was to have a belongsToMany relationship and attach a Model record to it.

What I have is a Filament app that records Animals via a tag number. Where i'm coming stuck is that the animal may give birth, so it then has a relation. I then create a record and I need to attach it as a relation to the current Animal record. Each thing I try I get errors or it doesn't show the records, but I'm sure I've messed up somewhere.

I have a Model called "Herd" which has a list of all the animals and has the usual columns of id, tag_number, name, dob and status. I have a pivot table setup named "herd_herd" with a "herd_id", "relation_id". I am trying to use this to attach a Animal to an animal as a relation. Hope this makes sense.

Ive tried a few different ways but it normally ends in not being able to select the related animal on the attach action.

The below code gives me this error when I try to attach or search in a relationship manager. Call to undefined method App\Models\Herd::herds()

Heres the Resource:

Model relation:

public function children()
    {
        return $this->belongsToMany(
            Herd::class,
            'herd_herd',
            'herd_id',
            'relation_id'
        );
    }

Any ideas or pointers would be grateful.

0 likes
2 replies
LaryAI's avatar
Level 58

To achieve a self-referential many-to-many relationship in your Herd model, you need to define both sides of the relationship. In your case, you want to relate animals to their offspring. You already have the children relationship defined, but you also need to define the inverse relationship, which could be called parents.

Here's how you can set it up:

  1. Model Setup:

    In your Herd model, define both the children and parents relationships:

    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Model;
    
    class Herd extends Model
    {
        // Other model properties and methods
    
        public function children()
        {
            return $this->belongsToMany(
                Herd::class,
                'herd_herd',
                'herd_id',
                'relation_id'
            );
        }
    
        public function parents()
        {
            return $this->belongsToMany(
                Herd::class,
                'herd_herd',
                'relation_id',
                'herd_id'
            );
        }
    }
    
  2. Resource Setup:

    In your Filament resource, you need to ensure that the relationship manager is correctly set up to handle these relationships. Here’s an example of how you can define the RelationRelationManager:

  3. Resource Configuration:

    Ensure that your HerdResource is correctly configured to include the RelationRelationManager:

This setup should allow you to manage the relationships between animals in your Herd model using Filament. The RelationRelationManager will handle the children relationship, allowing you to attach and manage related animals.

skoobi's avatar
Level 13

@LaryAI Unfortunately this gives me an error:

SQLSTATE[HY000]: General error: 1 table herds has no column named relation_id (Connection: sqlite, SQL: insert into "herds" ("relation_id", "updated_at", "created_at") values (2, 2024-09-24 07:56:36, 2024-09-24 07:56:36))

Please or to participate in this conversation.