Laravel's pivot table + Pivot table in general

后端 未结 4 1736

What are laravel pivot tables and pivot tables in general? What is this all about?

Recently I made research about Pivot table. I thought I know them and What they are bu

相关标签:
4条回答
  • 2021-02-12 20:52

    If you know about many-to-many relationships this is common, to handle many-to-many relationships we use intermediate (pivot) table to store relationships of two tables. Ex: consider about “education” and “person” tables which are listed below

    table: person

    |------|-------|-----|
    |  id  | name  | age |
    |------|-------|-----|
    |  1   | Bob   | 30  |
    |  2   | John  | 34  |
    |  3   | Marta | 28  |
    |------|-------|-----|
    

    table: education

    |------|-------|
    |  id  | level |
    |------|-------|
    |  1   | BSc   |
    |  2   | MSc   |
    |  3   | PhD   |
    |------|-------|
    

    Think that Bob has BSc, MSc and John has BSc, MSc, PhD and Marta has BSc, now this is consider as many-to-many relationship and to sort this relationship you need to have intermediate table such as,

    table: person_education

    |------------|--------------|
    |  person_id | education_id |
    |------------|--------------|
    |  1         |    1         |
    |  1         |    2         |
    |  2         |    1         |
    |  2         |    1         |
    |  2         |    3         |
    |  3         |    1         |
    |------------|--------------|
    

    This table mainly stores the primary keys (IDs) of each relationship. This is the basic idea behind the pivot table and when you use larval there are some best practises such as,

    • Name of the pivot table should consist of singular names of both tables, separated by undescore symbole and these names should be arranged in alphabetical order

    Laravel Ex:

    Class Person extends Model {
        public function education ()
        {   
          return $this->belongsToMany('App\Education', 'person_education');
        }
    }
    

    Moreover, you can specify the actual field names of that pivot table, if they are different than default person_id and education _id. Then just add two more parameters – first, the current model field, and then the field of the model being joined

    public function education() { 
      return $this->belongsToMany('App\Products', 'products_shops', 'shops_id', 'products_id'); 
    }
    
    0 讨论(0)
  • 2021-02-12 20:55

    Keep this in mind

    Pivot table — is a table used for connecting relationships between two tables.


    Laravel part — laravel provides many-to-many relationship where you can use pivot table, and it is very useful for many cases.

    Example:
    databases: users, post_user, posts

    User.php (Model)

    class User extends Model{
    
      public function posts(){
         return $this->belongsToMany('Post');
      }
    
    }
    

    Now, to access the all logged in user's posts: (view)

    @foreach(auth()->user()->posts as $post)
      <li>{{ $post->name }}</li>
    @endforeach
    

    Relationship happened:

    Remember we have post_user table which is the pivot table we used. If we have:

    user_id of 1 and we expect that it is the logged in user and post_id of 1, 2, 3, 4, all this posts will be printed out like so:

    |------------|--------------|
    |  post_id   |   user_id    |
    |------------|--------------|
    |  1         |    1         |
    |  2         |    1         |
    |  3         |    1         |
    |  4         |    1         |
    |------------|--------------|
    

    Output:

    • PostName1
    • PostName2
    • PostName3
    • PostName4
    0 讨论(0)
  • 2021-02-12 21:02

    When learning, focus only the pivot tables concept in Laravel (or eloquent). When I was learning I did not care about the general meaning of pivot table. I focused only on facts in the documentation (https://laravel.com/docs/5.5/eloquent-relationships#many-to-many)

    many-to-many relationships require an additional table.And we can insert other useful data to this table as well. And can be used as a model in the system.

    Example : User and Roles many-to-many relationship = User_roles

    Because of Pivot tables, you can retrieve intermediate table data as a model (like other models in the system).

    Example:

    //get user by id
    $user = App\User::find(1);
    
    //get roles of this user
    foreach ($user->roles as $role) {
    
      //pivot attribute returns a model which represent user_role table
      echo $role->pivot->created_at;
    
    }
    

    NOTE: you can create a class by extending pivot. But you have to implement the correct relationships to make it work. Your code should look somewhat similar to below code.

    class Student extends Model
    {
        /**
         * The users that belong to the role.
         */
        public function Rooms()
        {
            return $this->belongsToMany('App\Room')->using('App\Room_Student');
        }
    }
    
    class Room extends Model
    {
        /**
         * The users that belong to the role.
         */
        public function Students()
        {
            return $this->belongsToMany('App\Student')->using('App\Room_Student');
        }
    }
    
    class Room_Student extends Pivot
    {
        //
    }
    

    I hope this helps.

    0 讨论(0)
  • 2021-02-12 21:03

    Simply put is a pivot table a table that joins two tables together

    say you have a table users

    USERS:
    user_id, user_name
    

    say you have a table games

    GAMES
    game_id, game_name
    

    a user can play many games. games have many users playing them.

    To link them you make a third table

    GAMES_TO_USERS
    game_id, user_id
    

    with this table you can request which games a user plays, and which users play which game.

    this table GAMES_TO_USERS is in this case the pivot table.

    0 讨论(0)
提交回复
热议问题