Many To Many Relationships
To learn more about Many To Many Relationships visit our Bajarangisoft site
Attaching / Detaching
Eloquent also provides a few additional helper methods to make working with related models more convenient.
For example, let's imagine a Category can have many Products and a Products can have many Categories. To attach a product to a category by inserting a record in the intermediate table that joins the models, use the attach
method:
public function index()
{
$category = App\Category::find(1);
$category->product()->attach($product_id); // will return all categories for the product id 1
}
When attaching a relationship to a model, you may also pass an array of additional data to be inserted into the intermediate table:
public function index()
{
$category->product()->attach($product_id, ['name' => $name]);
}
Sometimes it may be necessary to remove a products from a Category. To remove a many-to-many relationship record, use the detach
method. The detach
method will delete the appropriate record out of the intermediate table; however, both models will remain in the database:
// Detach a single product from the category...
$category->product()->detach($product_id);
// Detach all products from the category...
$category->product()->detach();
For convenience, attach
and detach
also accept arrays of IDs as input:
public function index()
{
$category = App\Category::find(1);
$category->product()->detach([1, 2, 3]);
$user->product()->attach([
1 => ['quality' => $quality],
2 => ['quality' => $quality],
]);
}
Syncing Associations
You may also use the sync
method to construct many-to-many associations. The sync
method accepts an array of IDs to place on the intermediate table. Any IDs that are not in the given array will be removed from the intermediate table. So, after this operation is complete, only the IDs in the given array will exist in the intermediate table:
$category->products()->sync([1, 2, 3]);
You may also pass additional intermediate table values with the IDs:
$category->product()->sync([1 => ['name' => true], 2, 3]);
If you do not want to detach existing IDs, you may use the syncWithoutDetaching
method:
$category->product()->syncWithoutDetaching([1, 2, 3]);
Toggling Associations
The many-to-many relationship also provides a toggle
method which "toggles" the attachment status of the given IDs. If the given ID is currently attached, it will be detached. Likewise, if it is currently detached, it will be attached:
$category->product()->toggle([1, 2, 3]);
Saving Additional Data On A Pivot Table
When working with a many-to-many relationship, the save
method accepts an array of additional intermediate table attributes as its second argument:
App\Category::find(1)->product()->save($product, ['name' => $name]);
Updating A Record On A Pivot Table
If you need to update an existing row in your pivot table, you may use updateExistingPivot
method. This method accepts the pivot record foreign key and an array of attributes to update:
$category = App\Category::find(1);
$category->product()->updateExistingPivot($product_id, $attributes);