Table Pivot In Laravel

Tram Ho

1. Introduction

If you are new to Laravel, you may not know the concept of pivot table and its great use in applications. You might find it strange at first, but when you use it, it can be very useful for that many-to-many relationship ? .

2. Table Pivot

Basically, the pivot table is a common table between two main tables in many-to-many relationships and to better understand, we will first introduce the relationship through this many-to-many relationship! In fact, when doing small projects, we have at least met and used this relationship. Probably not right. Many-to-many is this relationship more complex than hasOne and hasMany. I will take the VD as 1 User may have many roles and 1 role also belongs to many users, a post has many tags, and a tag can also belong to many posts, a shop with many products and a product can be belong to many shops, …. I will take VD Shop and Product as an example in this article: First of all, to determine this relationship in addition to the main tables of shops and products, we will have one more table of time. is product_shop ( pivot table ). This table contains 2 foreign keys: shop_id and product_id.

Regarding the intermediate table naming rule in Eloquent:

  • The names of pivot tables include the names of two main tables in the singular form.
  • Separated by underscore (underscore).
  • Sort alphabetically ( Example: product_shop, user_role, … ).
  • Must contain at least 2 columns which are foreign keys of 2 main tables ( eg shop_id and product_id ). However, if you don’t like it, you can define the intermediate table name yourself.

In this article I will skip the step of creating migration with 3 tables shops, products and shop_product ? And next I will define the relations in each Model:

  • Model Shop

  • Many-to-many relationships are defined by calling the belongsToMany () method based on the Eloquent class.
  • As mentioned earlier, to determine the table name of the pivot table involved in relations, Eloquent will jion the two related models in alphabetical order. However, we can also override this convention if we wish, by passing the belongsToMany () method on the 22nd parameter Example: return $this->belongsToMany(Product::class, 'shop_product_id');
  • We can also customize foreign keys by adding third and fourth digits. The third argument is the foreign key you are defining the relationship for and the fourth is the foreign key in the model you’re joining. come.
  • If in the pivot table we have one more column, we will simply add the withPivot () function after defining relationships.

  • To retrieve the intermediate table columns we use the pivot attribute:

  • To determine the inverse of this much relationship, we just need to put a belongsToMany method on the other model of you.
  • It is also important to note that if you want your pivot table to automatically have created_at and updated_at timestamps, then we use the Timestamps methods in the definition of the relationship:

Actually in this article I would like to introduce to you that is manipulating the database through the three functions attach , detach and sync Previously before knowing these 3 methods I have done a slightly jail, that is create an intermediate table Model foreach one by one for each add, edit, delete ??? But now knowing the other e method you will not need such effort anymore hihi

1. attach This method is used in function stores, its main purpose is to add records to the intermediate table. For example, if you want to add more products to a specific shop, for example, just:

Simple is not it. In addition, if you want to add more records at once, we can pass a $ product_ids array, same for detach method too 2. detach This method uses in the delete and destroy functions. VD When you want to delete a product in a specific shop. If you want to delete more then we pass the array of $ shop_id

Also you want to remove all products in the shop

3. sync Another useful function is to update the entire pivot table which is the sync method. We often use it for function updates. Note here when using this method it only accepts the array ID passed and saved into the pivot table. Therefore, all IDs that are not in the array of IDs will be removed from the pivot table and in the pivot table will only remain the IDs in the input array: VD shop 1 has products 1,2,3,4 , 5. But I want to update it with products 1,3,5,7,9, we simply need:

So products 2.4 will be removed from the pivot table

3. Conclusion

Above is my introduction to the pivot table in laravel, thanks for reading.

Share the news now

Source : Viblo