Search code examples
phplaravelrelation

laravel Pivot table for Many to Many relation without laravel convention


I have 2 model user and team, every team can have multiple users and each user can be in any team , so I wanna make a many to many relation.

I'm trying to customize something , I don't wanna use the laravel convention for pivot table.

Here is my User Migration:

 class CreateUsersTable extends Migration
{
    protected $collection = "table_users";
    public function up()
    {
           Schema::create(  $this->collection
             , function (Blueprint $table) {

                    $table->string('_id');
                    $table->string('username')->unique();
                    $table->string('password');
             }
    }

}

And here is my Team Migration :

 class CreateTeamTable extends Migration
{
    protected $collection = "table_team";
    public function up()
    {
           Schema::create(  $this->collection
             , function (Blueprint $table) {

                    $table->string('_id');
                    $table->string('name')->unique();
             }
    }

}

So my problem is when I want to create method for the relation in User Model and Team Model.

in Team Model :

    public function users()
    {
        return $this->belongsToMany(User::class, '??', '??' , '??'); // what should I insert in '??'
    }

in User Model :

    public function teams()
    {
        return $this->belongsToMany(Team::class, '??', '??' , '??'); // what should I insert in '??'
    }

And also how to create Pivot table without using any convention:

pivot table that I created so far:

class CreateUsersTeamTable extends Migration
{
    protected $collection = "team_user_pivot";
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create(  $this->collection
            , function (Blueprint $table) {
            $table->id();
            $table->string('??');  // please help me to fill this field
            $table->string('??');  // please help me to fill this field
            $table->timestamps();
        });
    }

There are '??' in User Model and Team model and Pivot table . help me to fill up each of them


Solution

  • It's not quite as simple as filling in the fields, as you're using a string for the foreign key as well as not follwing the standard namig conventions.

    create_team_user_pivot

    protected $collection = "team_user_pivot";
    
    public function up()
    {
      Schema::create($this->collection, function (Blueprint $table) {
        $table->id();
        $table->string('team__id')->references('_id')->on('teams');
        $table->string('user__id')->references('_id')->on('users');
        $table->timestamps();
      });
    }
    

    You also need to edit the models:

    User

    class User extends Authenticatable
    {
        use HasApiTokens, HasFactory, Notifiable;
    
        public $table = 'table_users';
        public $incrementing = false;
        public $keyType = 'string';
        protected $primaryKey = '_id';
    
        protected $fillable = [
            "_id",
            "username",
            "password"
        ];
    
        public function teams()
        {
            return $this->belongsToMany(Team::class, 'team_user_pivot', 'user__id', 'team__id');
        }
    }
    

    Team

    class Team extends Model
    {
        use HasFactory;
    
        public $table = 'table_teams';
        public $incrementing = false;
        public $keyType = 'string';
        protected $primaryKey = '_id';
    
        protected $guarded = [];
    
        public function users()
        {
            return $this->belongsToMany(User::class, 'team_user_pivot', 'team__id', 'user__id');
        }
    }