
Not anymore - there's a method called sync() which accept new values as parameters array, and then takes care of all that "dirty work" of syncing:
Laravel eloquent update pivot table update#
Really often example - in your admin area there are checkboxes for shops for a particular product, and on Update operation you actually have to check all shops, delete those which are not in new checkbox array, and then add/update existing ones. $shop->products()->detach() Īnd another REALLY useful function, in my experience, is updating the whole pivot table. You can also attach and detach rows, passing array of values as parameters:

Or, more brutally, remove all products from a particular shop - then just call method without parameters: Likewise, we can detach a relationship - let's say, we want to remove a product from the shop: The result - a new row will be added to product_shop table, with $product_id and $shop_id values.

Now, how do we actually save the data with a help of our two Models instead of the third intermediate one? Couple of things here.įor example, if we want to add another product to the current shop instance, we use relationship function and then method attach(): So, we have tables, and we have Models ready.

Managing Many-to-Many Relationships: attach-detach-sync One of the main benefits here: you don't need to create a separate model for ProductShop - you will be able to manage that table through pivot commands, we will discuss that right away. Return $this->belongsToMany(Product::class, 'products_shops', Then just add two more parameters - first, the current model field, and then the field of the model being joined: Moreover, you can specify the actual field names of that pivot table, if they are different than default product_id and shop_id. Return $this->belongsToMany(Product::class, 'products_shops') But, if it's actually different (for example, it's plural), you can provide it as a second parameter: Now, with such declaration of relationships Laravel "assumes" that pivot table name obeys the rules and is product_shop. Return $this->belongsToMany(Shop::class) Īctually, you can do both - it depends on how will you actuall use the relationship in other parts of the code: will you need $shop->products or more likely to query $product->shops, or both. Return $this->belongsToMany(Product::class) Īpp/Models/Product.php: class Product extends Model The main part here is to assign a many-to-many relationship - it can be done from either of "main" tables models.Īpp/Models/Shop.php: class Shop extends Model Ok, we have DB tables and migrations, now let's create models for them. Models for Many-to-Many Relationships: BelongsToMany You can add more fields if you want, then you need to add them to relationship assignment - we will discuss that later.

Pivot table is an example of intermediate table with relationships between two other "main" tables. New field Amount: Migration/Modelįrom the default Generated QuickAdminPanel, we have this structure.Today I want to talk about a feature of Laravel which is really useful but can be potentially difficult to understand at first. So, let’s perform the change, step-by-step.
