Laravel 5 Eloquent Many to Many 2ndary table

Joseph Michael

I have a question that I'm not sure how to solve or even phrase for finding an answer.

I have a Company Model & User Model that are related Many-to-Many.

I want to have a user_pins table. A user can belong to multiple companies and therefore have a different pin within each company. The pin is unique within a company, no two users within a company can have the same one. Users in different companies can have the same one.

So for the company it is One to Many, for the user it is One to Many, but altogether it is many to many, Im not sure if that makes sense.

I have the table set up as

Schema::create('user_pins', function (Blueprint $table) {
 $table->integer('user_id')->unsigned();
 $table->integer('company_id')->unsigned();
 $table->string('pin');

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

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

How do I relate this table in the models and use Eloquent to access/create/update it so it stores both the user and company?

Rwd

Firstly, I would change the name to company_user so that it follows the same naming convention that Laravel would use out of the box. (you wouldn't have to do this as you can specify the pivot table name in the relationship but if there isn't a reason to stick with user_pin it makes sence to follow convention :) )

Then I would remove the primary key from being a compound of all 3 fields and just have it on the company_id and user_id.

Lastly, as a PIN only has to be unique for a company, I would just put the unique index on those two columns e.g.

Schema::create('company_user', function (Blueprint $table) {

    $table->integer('company_id')->unsigned()->index();
    $table->integer('user_id')->unsigned()->index();
    $table->string('pin');

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

    $table->primary(['company_id', 'user_id']);
    $table->unique(['company_id', 'pin']);
});

Then for the relationship in the model I would have something like:

return $this->belongsToMany('App\Company')->withPivot('pin');

and

return $this->belongsToMany('App\User')->withPivot('pin');

Examples of use with pivot

All user pins for a company:

$company->users->lists('pivot.pin');

Users pin for a specific company

$user->companies()->where('id', $id)->get()->pivot->pin;

Users pin for the first company relationship:

$user->companies->first()->pivot->pin;

Hope this helps!

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 ORM - Many to Many through Many to Many

From Dev

Laravel Eloquent many to many filter on intermediate table

From Dev

Laravel 5 Eloquent count many to many relationship

From Dev

Laravel eloquent - Many to Many, select only matching the many table

From Dev

Should eloquent's many-to-many pivot table enforce uniqueness accross the two id columns? Laravel 5

From Dev

Laravel 5 Many to Many - Table name in singular

From Dev

Laravel eloquent ,has many in multi table

From Dev

Laravel 5 Eloquent Relationship - Has Many Though

From Dev

Laravel 5 one to many eloquent relationship

From Dev

Laravel 5 Eloquent Relationship - Has Many Though

From Dev

Laravel - Eloquent relation - many-to-many - get intermediate table columns

From Dev

Laravel Eloquent Many to Many relationship using a pivot table

From Dev

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

From Dev

Laravel Eloquent Many to Many Query

From Dev

Laravel many to many selecting with Eloquent

From Dev

Laravel eloquent many to many to many model

From Dev

Laravel Eloquent one to many

From Dev

Eloquent - Many to many where not found in the pivot table

From Dev

Laravel Eloquent many to many how to add where?

From Dev

Laravel 4 Many to Many with Eloquent and checkboxes

From Dev

Laravel Eloquent Many-To-Many distinct

From Dev

Laravel many to many relationship using Eloquent

From Dev

Laravel Eloquent Many-to-Many query

From Dev

Laravel Eloquent "Many-to-Many-to-One ?"

From Dev

Laravel Eloquent Many-To-Many distinct

From Dev

Laravel Eloquent join three table-has many through

From Dev

laravel many to many with third table

From Dev

Laravel 5 Eloquent relation "Has Many Through" SQL Exception

From Dev

Laravel Eloquent ORM - Many to Many Delete Pivot Table Values left over

Related Related

  1. 1

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

  2. 2

    Laravel Eloquent many to many filter on intermediate table

  3. 3

    Laravel 5 Eloquent count many to many relationship

  4. 4

    Laravel eloquent - Many to Many, select only matching the many table

  5. 5

    Should eloquent's many-to-many pivot table enforce uniqueness accross the two id columns? Laravel 5

  6. 6

    Laravel 5 Many to Many - Table name in singular

  7. 7

    Laravel eloquent ,has many in multi table

  8. 8

    Laravel 5 Eloquent Relationship - Has Many Though

  9. 9

    Laravel 5 one to many eloquent relationship

  10. 10

    Laravel 5 Eloquent Relationship - Has Many Though

  11. 11

    Laravel - Eloquent relation - many-to-many - get intermediate table columns

  12. 12

    Laravel Eloquent Many to Many relationship using a pivot table

  13. 13

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

  14. 14

    Laravel Eloquent Many to Many Query

  15. 15

    Laravel many to many selecting with Eloquent

  16. 16

    Laravel eloquent many to many to many model

  17. 17

    Laravel Eloquent one to many

  18. 18

    Eloquent - Many to many where not found in the pivot table

  19. 19

    Laravel Eloquent many to many how to add where?

  20. 20

    Laravel 4 Many to Many with Eloquent and checkboxes

  21. 21

    Laravel Eloquent Many-To-Many distinct

  22. 22

    Laravel many to many relationship using Eloquent

  23. 23

    Laravel Eloquent Many-to-Many query

  24. 24

    Laravel Eloquent "Many-to-Many-to-One ?"

  25. 25

    Laravel Eloquent Many-To-Many distinct

  26. 26

    Laravel Eloquent join three table-has many through

  27. 27

    laravel many to many with third table

  28. 28

    Laravel 5 Eloquent relation "Has Many Through" SQL Exception

  29. 29

    Laravel Eloquent ORM - Many to Many Delete Pivot Table Values left over

HotTag

Archive