Laravel 5 Eloquent Relationship - Has Many Though

user3463927

I have three models.

  • Study
  • Site
  • Unit

Study Has and belongs to many Sites and each site that belongs to Study again has and belongs to many Unit. Please see the following drawing.

http://tinypic.com/r/ojhx0g/8

How I achieve this using Laravel 5 Eloquent Relationships.

BrokenBinary

It sounds like you have many-to-many relationships between Study->Site and Site->Unit. You can read the Laravel documentation about many-to-many relationships here.

Models

Here are the relevant functions you'll need for Eloquent to recognize the relationships.

class Study extends Model {
    // If you named your table differently (like 'studies'), specify that here
    protected $table = 'studys';

    // This assumes a pivot table named 'site_study', if you named yours
    // differently you can pass in into the belongsToMany() function as the
    // second parameter.
    public function sites() {
        return $this->belongsToMany('App\Site');
    }
}

class Site extends Model {
    protected $table = 'sites';

    public function studies() {
        return $this->belongsToMany('App\Study');
    }

    public function units() {
        return $this->belongsToMany('App\Unit');
    }
}

class Unit extends Model {
    protected $table = 'units';

    public function sites() {
        return $this->belongsToMany('App\Site');
    }
}

Then, to access the Sites belonging to a Study you would do this:

$sites = Study::find(1)->sites;

Pivot Table Migrations

Laravel expects pivot tables to be named like 'alpha_beta' where alpha and beta are the singular model names in alphabetical order. So your migrations for the pivot tables would look like this:

class CreateSiteStudyTable extends Migration {
    public function up() {
        Schema::create('site_study', function(Blueprint $table)) {
            $table->integer('site_id')->unsigned();
            $table->foreign('site_id')->references('id')->on('sites');
            $table->integer('study_id')->unsigned();
            $table->foreign('study_id')->references('id')->on('studys'); // or whatever you named it
            $table->unique(['site_id', 'study_id']);
            $table->timestamps();
        });
    }

    public function down() {
        Schema::drop('site_study');
    }
}

class CreateSiteUnitTable extends Migration {
    public function up() {
        Schema::create('site_unit', function(Blueprint $table)) {
            $table->integer('site_id')->unsigned();
            $table->foreign('site_id')->references('id')->on('sites');
            $table->integer('unit_id')->unsigned();
            $table->foreign('unit_id')->references('id')->on('units');
            $table->unique(['site_id', 'unit_id']);
            $table->timestamps();
        });
    }

    public function down() {
        Schema::drop('site_unit');
    }
}

You can read about Foreign Keys in Laravel here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Laravel 5 Eloquent Relationship - Has Many Though

From Dev

Laravel 5 Eloquent count many to many relationship

From Dev

Laravel 5 one to many eloquent relationship

From Dev

has_many though relationship does not work

From Dev

Laravel many to many relationship using Eloquent

From Dev

Laravel 5 Eloquent relation "Has Many Through" SQL Exception

From Dev

Laravel 5 Eloquent relationship belongsToMany

From Dev

Eloquent ORM relationship on laravel 5

From Dev

Laravel Eloquent relationship (5 tables)

From Dev

laravel update, remove one to many relationship eloquent

From Dev

Laravel eloquent update one-many relationship

From Dev

Laravel eloquent update one-many relationship

From Dev

One to Many to One relationship in laravel eloquent

From Dev

Laravel 5 | Many to Many Relationship not working

From Dev

Eager Loading has_many though on a belongs_to relationship

From Dev

Eager Loading has_many though on a belongs_to relationship

From Dev

Laravel save many-to-many relationship in Eloquent mutators

From Dev

Laravel Eloquent: How to select not attached records in many to many relationship?

From Dev

Need help to structure Eloquent Many to Many relationship (laravel 5.1)

From Dev

Laravel 5.1 Eloquent distant relationship with many to many models

From Dev

Laravel Eloquent Many to Many relationship using a pivot table

From Dev

Laravel 5.2 Eloquent - Model through Many-To-Many Relationship

From Dev

Laravel Eloquent many-to-many relationship: Use explicit pivot table

From Dev

Many to many Laravel Eloquent relationship with multiple intermediate tables

From Dev

Laravel 5 Eloquent ORM - Many to Many through Many to Many

From Dev

Laravel eloquent ,has many in multi table

From Dev

Laravel count number of rows in has many relationship

From Dev

Laravel posts and tags: has many through relationship?

From Dev

Laravel count number of rows in has many relationship

Related Related

  1. 1

    Laravel 5 Eloquent Relationship - Has Many Though

  2. 2

    Laravel 5 Eloquent count many to many relationship

  3. 3

    Laravel 5 one to many eloquent relationship

  4. 4

    has_many though relationship does not work

  5. 5

    Laravel many to many relationship using Eloquent

  6. 6

    Laravel 5 Eloquent relation "Has Many Through" SQL Exception

  7. 7

    Laravel 5 Eloquent relationship belongsToMany

  8. 8

    Eloquent ORM relationship on laravel 5

  9. 9

    Laravel Eloquent relationship (5 tables)

  10. 10

    laravel update, remove one to many relationship eloquent

  11. 11

    Laravel eloquent update one-many relationship

  12. 12

    Laravel eloquent update one-many relationship

  13. 13

    One to Many to One relationship in laravel eloquent

  14. 14

    Laravel 5 | Many to Many Relationship not working

  15. 15

    Eager Loading has_many though on a belongs_to relationship

  16. 16

    Eager Loading has_many though on a belongs_to relationship

  17. 17

    Laravel save many-to-many relationship in Eloquent mutators

  18. 18

    Laravel Eloquent: How to select not attached records in many to many relationship?

  19. 19

    Need help to structure Eloquent Many to Many relationship (laravel 5.1)

  20. 20

    Laravel 5.1 Eloquent distant relationship with many to many models

  21. 21

    Laravel Eloquent Many to Many relationship using a pivot table

  22. 22

    Laravel 5.2 Eloquent - Model through Many-To-Many Relationship

  23. 23

    Laravel Eloquent many-to-many relationship: Use explicit pivot table

  24. 24

    Many to many Laravel Eloquent relationship with multiple intermediate tables

  25. 25

    Laravel 5 Eloquent ORM - Many to Many through Many to Many

  26. 26

    Laravel eloquent ,has many in multi table

  27. 27

    Laravel count number of rows in has many relationship

  28. 28

    Laravel posts and tags: has many through relationship?

  29. 29

    Laravel count number of rows in has many relationship

HotTag

Archive