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

brianbola90's avatar

Relationship between 2 pivot tables

Im trying to build an app where a user is part of a wedding and has a role in that wedding. Im using entrust for the roles and permissions. I have a user model a role model and a wedding model.

<?php
namespace App;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Zizaco\Entrust\Traits\EntrustUserTrait;
class User extends Authenticatable
{
    use EntrustUserTrait;
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function Weddings()
    {
        return $this->belongsToMany(Wedding::class, 'user_weddings');
   }
}

Wedding Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Wedding extends Model
{
    protected $table='weddings';
    protected $fillable = array('name', 'description');

    public function Users()
    {
        return $this->belongsToMany(User::class, 'user_weddings');
    }


}

Role Model

<?php
/**
 * Created by PhpStorm.
 * User: Userdddddddddddddddd
 * Date: 19/04/2016
 * Time: 21:18
 */

namespace App;

use Zizaco\Entrust\EntrustRole;

class Role extends EntrustRole
{

}

User_wedding table

class UserWedding extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('user_weddings', function(Blueprint $table)
        {
            $table->integer('user_id')->unsigned();
            $table->integer('wedding_id')->unsigned();

            $table->foreign('user_id')->references('id')->on('users')
                ->onUpdate('cascade')->onDelete('cascade');
            $table->foreign('wedding_id')->references('id')->on('weddings')
                ->onUpdate('cascade')->onDelete('cascade');



        });
    }

User_role table

 // Create table for associating roles to users (Many-to-Many)
        Schema::create('role_user', function (Blueprint $table) {
            $table->integer('user_id')->unsigned();
            $table->integer('role_id')->unsigned();

            $table->foreign('user_id')->references('id')->on('users')
                ->onUpdate('cascade')->onDelete('cascade');
            $table->foreign('role_id')->references('id')->on('roles')
                ->onUpdate('cascade')->onDelete('cascade');

            $table->primary(['user_id', 'role_id']);
        });

Ideally I would be able to add a role_id column to user_wedding but cant figure it out. How do I create a eloquent query that will return a user that only has a role of 4 that belongs to that wedding? I cant figure out how to build a realtionship for this.

0 likes
1 reply

Please or to participate in this conversation.