Skip to content

Commit

Permalink
Merge pull request #37 from alvin-nt/patch-1
Browse files Browse the repository at this point in the history
Update 3-many-to-many.md
  • Loading branch information
chelout authored Jul 17, 2020
2 parents 3f8d013 + a0232be commit 21a7ad1
Showing 1 changed file with 68 additions and 67 deletions.
135 changes: 68 additions & 67 deletions doc/3-many-to-many.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,60 +27,71 @@ 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()
);

// ...
```

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}.");
});
}
// ...
```

### 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

Expand All @@ -106,67 +117,57 @@ 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);

// ...
```

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}.");
});
}
// ...
```

### 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

0 comments on commit 21a7ad1

Please sign in to comment.