chelout/laravel-relationship-events
Adds missing Eloquent relationship events to Laravel models. Use simple traits (HasOne/Many, BelongsTo/Many, Morph*) to listen for attach/detach/sync, saved/updated, and other relation lifecycle hooks with parent/related context and IDs/attributes.
User model might be associated with one Address
namespace App;
use Illuminate\Database\Eloquent\Model;
use Chelout\RelationshipEvents\Concerns\HasMorphOneEvents;
class User extends Model
{
use HasMorphOneEvents;
/**
* Get the address associated with the user.
*/
public function address()
{
return $this->morphOne(Address::class, 'addressable');
}
}
Now we can use methods to assosiate User with Address and also update assosiated model.
// ...
$user = factory(User::class)->create([
'name' => 'John Smith',
]);
// Create address and assosiate it with user
// This will fire two events morphOneCreating, morphOneCreated
$user->address()->create([
'phone' => '8-800-123-45-67',
'email' => 'user@example.com',
'address' => 'One Infinite Loop Cupertino, CA 95014',
]);
// ...
Now we should listen our events, for example we can register event listners in model's boot method:
// ...
protected static function boot()
{
parent::boot();
static::morphOneCreating(function ($parent, $related) {
Log::info("Creating address for user {$parent->name}.");
});
static::morphOneCreated(function ($parent, $related) {
Log::info("Address for user {$parent->name} has been created.");
});
}
// ...
There is also inverse relation Address belongs to User
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Chelout\RelationshipEvents\Concerns\HasBelongsToEvents;
class Address extends Model
{
use HasBelongsToEvents;
/**
* Get the user that owns address.
*/
public function user()
{
return $this->morphTo(User::class);
}
}
Now we can use methods to assosiate, dissosiate and update User with Address:
// ...
$user = factory(User::class)->create([
'name' => 'John Smith',
]);
$address = factory(Address::class)->create([
'phone' => '8-800-123-45-67',
'email' => 'user@example.com',
'address' => 'One Infinite Loop Cupertino, CA 95014',
]);
// Assosiate address with user
// This will fire two events morphToAssociating, morphToAssociated
$address->user()->assosiate($user);
// ...
Now we should listen our events, for example we can register event listners in model's boot method:
// ...
protected static function boot()
{
parent::boot();
static::morphToAssociating(function ($relation, $related, $parent) {
Log::info("Associating user {$parent->name} with address.");
});
static::morphToAssociated(function ($relation, $related, $parent) {
Log::info("Address has been assosiated with user {$parent->name}.");
});
}
// ...
Note: related model is dirty, should be saved after associating
Note: has additional query to get parent model Note: related model is dirty, should be saved after dissociating
Note: has additional query to get related model
How can I help you explore Laravel packages today?