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

inilabs's avatar

How to use eloquent in multiple Model many to many relationship

Hi, i have four table 'students', 'locations', 'zones', 'class' and one mapping table 'mapping' for all. Now how can i get all students of a particular zone or a location or wave?

Schema::create('students', function (Blueprint $table) { $table->increments('id'); $table->string('name'); $table->integer('age'); $table->timestamps(); });

Schema::create('locations', function (Blueprint $table) { $table->increments('id'); $table->integer('zone_id'); $table->string('location'); $table->timestamps(); });

Schema::create('zones', function (Blueprint $table) { $table->increments('id'); $table->string('zone'); $table->timestamps(); }); Schema::create('waves', function (Blueprint $table) { $table->increments('id'); $table->string('wave'); $table->timestamps(); });

Schema::create('maps', function (Blueprint $table) { $table->increments('id'); $table->integer('location_id'); $table->integer('zone_id'); $table->integer('wave_id'); $table->integer('student_id'); $table->timestamps(); });

0 likes
5 replies
willvincent's avatar

Formatting your code so it's readable...

Schema::create('students', function (Blueprint $table) {
  $table->increments('id');
  $table->string('name');
  $table->integer('age');
  $table->timestamps();
});

Schema::create('locations', function (Blueprint $table) {
  $table->increments('id');
  $table->integer('zone_id');
  $table->string('location');
  $table->timestamps();
});

Schema::create('zones', function (Blueprint $table) {
  $table->increments('id');
  $table->string('zone');
  $table->timestamps();
});

Schema::create('waves', function (Blueprint $table) {
  $table->increments('id');
  $table->string('wave');
  $table->timestamps();
});

Schema::create('maps', function (Blueprint $table) {
  $table->increments('id');
  $table->integer('location_id');
  $table->integer('zone_id');
  $table->integer('wave_id');
  $table->integer('student_id');
  $table->timestamps();
});

For future reference, put three backticks on the line before and after each block of code so that it actually remains readable. ;)

1 like
willvincent's avatar

What do the relationships look like in your models? Given how overloaded your pivot is, you probably would be best off have a model for the maps then you could just say:

$students = Map::where('wave_id', '=', 5)->get();

Otherwise, while probably doable, I think you'll find it difficult to manage 4-way many to many relationships.

inilabs's avatar

@willvincent i know it will be difficult, but i need this to be done cause i have this kind of relationship in my app and i have created 4 model for every table but i does not created mapping table model. Now i did not get how to resolve this issue.

inilabs's avatar

i have get the data doing this, but i am confused that this is the right way or not..

class Zone extends Model
{
    protected $fillable = array('zone');

    public function locations() {
        return $this->hasMany('App\Location');
    }
    public function students()
    {
        return $this->belongsToMany('App\Student', 'maps', 'zone_id', 'student_id');
    }
    public function waves() {
        return $this->belongsToMany('App\Wave', 'maps', 'zone_id', 'wave_id');
    }
}

class Location extends Model
{
    protected $fillable = array('zone_id', 'location');
    public function zone()
    {
        return $this->belongsTo('App\Zone');
    }
    public function students()
    {
        return $this->belongsToMany('App\Student', 'maps', 'location_id', 'student_id');
    }
    public function waves()
    {
        return $this->belongsToMany('App\Wave', 'maps', 'location_id', 'wave_id');
    }
}

class Student extends Model
{
    protected $fillable = array('name', 'age');

    public function waves()
    {
        return $this->belongsToMany('App\wave', 'maps', 'student_id', 'wave_id');
    }
    public function locations()
    {
        return $this->belongsToMany('App\Location', 'maps', 'student_id', 'location_id');
    }
    public function zones()
    {
        return $this->belongsToMany('App\Zone', 'maps', 'student_id', 'zone_id');
    }
}

class Wave extends Model
{
    protected $fillable = array('wave');

    public function students()
    {
        return $this->belongsToMany('App\Student', 'maps', 'wave_id', 'student_id');
    }
    public function locations() {
        return $this->belongsToMany('App\Location', 'maps', 'wave_id', 'location_id');
    }
    public function zones() {
        return $this->belongsToMany('App\Zone', 'maps', 'wave_id', 'zone_id');
    }
}

// In Route File
  Route::get('zone/all', function (){
    $zones = Zone::where('zone', '=', 'Dhaka')->first();
    echo "Locations : ";
    echo $zones->locations->count()."<br>";
    echo "Students Counter<br>";
    foreach ($zones->locations as $location) {
        echo $location->location.": ";
        echo count($location->students)."<br>";
    }


});

Please or to participate in this conversation.