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

inilabs's avatar

Five way many to many relationship with elequent

I have 5 table, how can i get my required data with minimum query -

CREATE TABLE `locations` (
  `id` int(10) unsigned NOT NULL,
  `zone_id` int(11) NOT NULL,
  `location` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


CREATE TABLE `maps` (
  `id` int(10) unsigned NOT NULL,
  `location_id` int(11) NOT NULL,
  `wave_id` int(11) NOT NULL,
  `student_id` int(11) NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE `students` (
  `id` int(10) unsigned NOT NULL,
  `name` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `age` int(11) NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


CREATE TABLE `waves` (
  `id` int(10) unsigned NOT NULL,
  `wave` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


CREATE TABLE `zones` (
  `id` int(10) unsigned NOT NULL,
  `zone` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `created_at` timestamp NULL DEFAULT NULL,
  `updated_at` timestamp NULL DEFAULT NULL
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

Now how can i get all information of zone 1 with all locations of that zone and all wave of those locations and all students of that wave and location.

and i also i need to know all students of that zone..

this is my models looks like now..

class Location extends Model
{
    protected $fillable = array('zone_id', 'location');
    public function zone()
    {
        return $this->belongsTo(Zone::class);
    }
    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 Zone extends Model
{
    protected $fillable = array('zone');

    public function locations() {
        return $this->hasMany(Location::class);
    }
}

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');
    }
}
0 likes
1 reply

Please or to participate in this conversation.