diff --git a/doc/3-many-to-many.md b/doc/3-many-to-many.md index ecb3f34..eff8ec6 100644 --- a/doc/3-many-to-many.md +++ b/doc/3-many-to-many.md @@ -27,20 +27,30 @@ class User extends Model } ``` -Now we can use methods to assosiate ```Country``` with ```User``` and also update assosiated models. +Now we can use attach, detach, sync, toggle methods to update existing pivot: ```php // ... - -// create user $user = factory('App\User')->create(); -// Getting random country -$country = App\Models\Country::inRandomOrder()->first(); +// Attcha one role to user +// This will fire two events belongsToManyAttaching, belongsToManyAttached +$role = factory('App\Models\Role')->create(); +$user->roles()->attach($role, ['is_active' => true]); -// Saving user with specified country -// This will fire two events hasManySaving, hasManySaved -$country->users()->save($user); +// Attach many roles to user +// This will fire two events belongsToManyAttaching, belongsToManyAttached +$roles = factory('App\Models\Role', 2)->create(); +$user->roles()->attach( + $roles->mapWithKeys(function ($role) { + return [ + $role->id => [ + 'is_active' => true, + ], + ]; + }) + ->toArray() +); // ... ``` @@ -48,20 +58,16 @@ $country->users()->save($user); Now we should listen our events, for example we can register event listners in model's boot method: ```php // ... - public static function boot() + protected static function boot() { parent::boot(); - /** - * One To Many Relationship Events - */ - - static::hasManySaving(function ($parent, $related) { - Log::info("Saving user's country {$parent->name}."); + static::belongsToManyAttaching(function ($relation, $parent, $ids) { + Log::info("Attaching roles to user {$parent->name}."); }); - static::hasManySaved(function ($parent, $related) { - Log::info("User's country is now set to {$parent->name}."); + static::belongsToManyAttached(function ($relation, $parent, $ids) { + Log::info("Roles has been attached to user {$parent->name}."); }); } // ... @@ -69,18 +75,23 @@ Now we should listen our events, for example we can register event listners in m ### Available methods and events -#### HasMany::create (HasOneOrMany::create) -- fires hasManyCreating, hasManyCreated -- events have $parent and $related models - -#### HasMany::save (HasOneOrMany::save) -- fires hasManyCreating, hasManyCreated -- events have $parent and $related models +#### BelongsToMany::attach +- fires belongToManyAttaching, belongToManyAttached +- events have $relation name, $parent model, $attributes attaching model ids +#### BelongsToMany::detach +- fires belongToManyDetaching, belongToManyDetached +- events have $relation name, $parent model, $ids detaching model ids, $attributes additional data +> Note: has additional query to get related ids +#### BelongsToMany::sync +- fires belongToManySyncing, belongToManySynced, BelongsToMany::attach, BelongsToMany::detach +- events have $relation name, $parent model, $ids detaching model ids, $attributes additional data +#### BelongsToMany::toggle +- fires belongToManyToggling, belongToManyToggled, BelongsToMany::attach, BelongsToMany::detach +- events have $relation name, $parent model, $ids detaching model ids, $attributes additional data +#### BelongsToMany::updateExistingPivot +- fires belongsToManyUpdatingExistingPivot, belongsToManyUpdatedExistingPivot +- events have $relation name, $parent model, $id updating model id, $attributes additional data -#### HasMany::update (HasOneOrMany::update) -- fires hasManyUpdating, hasManyUpdated -- events have $parent model and $related Eloquent collection -> Note: has additional query to get related Eloquent collection ## belongsTo @@ -106,30 +117,20 @@ class User extends Model } ``` -Now we can use methods to attach, detach, sync, toggle and update existing pivot: +Now we can use methods to assosiate ```Country``` with ```User``` and also update assosiated models. ```php // ... + +// create user $user = factory('App\User')->create(); -// Attcha one role to user -// This will fire two events belongsToManyAttaching, belongsToManyAttached -$role = factory('App\Models\Role')->create(); -$user->roles()->attach($role, ['is_active' => true]); +// Getting random country +$country = App\Models\Country::inRandomOrder()->first(); -// Attach many roles to user -// This will fire two events belongsToManyAttaching, belongsToManyAttached -$roles = factory('App\Models\Role', 2)->create(); -$user->roles()->attach( - $roles->mapWithKeys(function ($role) { - return [ - $role->id => [ - 'is_active' => true, - ], - ]; - }) - ->toArray() -); +// Saving user with specified country +// This will fire two events hasManySaving, hasManySaved +$country->users()->save($user); // ... ``` @@ -137,16 +138,20 @@ $user->roles()->attach( Now we should listen our events, for example we can register event listners in model's boot method: ```php // ... - protected static function boot() + public static function boot() { parent::boot(); - static::belongsToManyAttaching(function ($relation, $parent, $ids) { - Log::info("Attaching roles to user {$parent->name}."); + /** + * One To Many Relationship Events + */ + + static::hasManySaving(function ($parent, $related) { + Log::info("Saving user's country {$parent->name}."); }); - static::belongsToManyAttached(function ($relation, $parent, $ids) { - Log::info("Roles has been attached to user {$parent->name}."); + static::hasManySaved(function ($parent, $related) { + Log::info("User's country is now set to {$parent->name}."); }); } // ... @@ -154,19 +159,15 @@ Now we should listen our events, for example we can register event listners in m ### Available methods and events -#### BelongsToMany::attach -- fires belongToManyAttaching, belongToManyAttached -- events have $relation name, $parent model, $attributes attaching model ids -#### BelongsToMany::detach -- fires belongToManyDetaching, belongToManyDetached -- events have $relation name, $parent model, $ids detaching model ids, $attributes additional data -> Note: has additional query to get related ids -#### BelongsToMany::sync -- fires belongToManySyncing, belongToManySynced, BelongsToMany::attach, BelongsToMany::detach -- events have $relation name, $parent model, $ids detaching model ids, $attributes additional data -#### BelongsToMany::toggle -- fires belongToManyToggling, belongToManyToggled, BelongsToMany::attach, BelongsToMany::detach -- events have $relation name, $parent model, $ids detaching model ids, $attributes additional data -#### BelongsToMany::updateExistingPivot -- fires belongsToManyUpdatingExistingPivot, belongsToManyUpdatedExistingPivot -- events have $relation name, $parent model, $id updating model id, $attributes additional data +#### HasMany::create (HasOneOrMany::create) +- fires hasManyCreating, hasManyCreated +- events have $parent and $related models + +#### HasMany::save (HasOneOrMany::save) +- fires hasManyCreating, hasManyCreated +- events have $parent and $related models + +#### HasMany::update (HasOneOrMany::update) +- fires hasManyUpdating, hasManyUpdated +- events have $parent model and $related Eloquent collection +> Note: has additional query to get related Eloquent collection