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

earmsby's avatar

two different relationships between two models

I'm sure this is do-able but I'm hazy on how best to accomplish it.

I have two models: accounts & works

The works model has a many-to-many relationship to accounts as author(s) of the work. But it also has a separate many-to-many relationship with accounts as publisher(s) of the work. How would it be possible to define something like works->authors and works->publishers which both link between works and accounts?

Thanks in advance for any tips.

0 likes
1 reply
LaryAI's avatar
LaryAI
AI
Best Answer
Level 58

To achieve this, you can define two separate many-to-many relationships in your Work model, each using a different pivot table. You'll need two pivot tables: one for authors and one for publishers. Here's how you can set it up:

  1. Create the Pivot Tables:

    You'll need two pivot tables, for example, account_work_authors and account_work_publishers. You can create these using migrations:

    // Migration for account_work_authors
    Schema::create('account_work_authors', function (Blueprint $table) {
        $table->id();
        $table->foreignId('work_id')->constrained()->onDelete('cascade');
        $table->foreignId('account_id')->constrained()->onDelete('cascade');
        $table->timestamps();
    });
    
    // Migration for account_work_publishers
    Schema::create('account_work_publishers', function (Blueprint $table) {
        $table->id();
        $table->foreignId('work_id')->constrained()->onDelete('cascade');
        $table->foreignId('account_id')->constrained()->onDelete('cascade');
        $table->timestamps();
    });
    
  2. Define Relationships in the Work Model:

    In your Work model, define the relationships using the belongsToMany method, specifying the pivot table for each relationship.

    class Work extends Model
    {
        public function authors()
        {
            return $this->belongsToMany(Account::class, 'account_work_authors');
        }
    
        public function publishers()
        {
            return $this->belongsToMany(Account::class, 'account_work_publishers');
        }
    }
    
  3. Define Relationships in the Account Model:

    Similarly, you can define inverse relationships in the Account model if needed.

    class Account extends Model
    {
        public function authoredWorks()
        {
            return $this->belongsToMany(Work::class, 'account_work_authors');
        }
    
        public function publishedWorks()
        {
            return $this->belongsToMany(Work::class, 'account_work_publishers');
        }
    }
    
  4. Usage:

    You can now access the authors and publishers of a work like this:

    $work = Work::find(1);
    
    // Get authors
    $authors = $work->authors;
    
    // Get publishers
    $publishers = $work->publishers;
    

This setup allows you to maintain two distinct many-to-many relationships between the Work and Account models, one for authors and another for publishers.

Please or to participate in this conversation.