PHP code example of chelout / laravel-relationship-events
1. Go to this page and download the library: Download chelout/laravel-relationship-events library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
chelout / laravel-relationship-events example snippets
use Chelout\RelationshipEvents\Concerns\HasOneEvents;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use HasOneEvents;
public static function boot()
{
parent::boot();
/**
* One To One Relationship Events
*/
static::hasOneSaved(function ($parent, $related) {
dump('hasOneSaved', $parent, $related);
});
static::hasOneUpdated(function ($parent, $related) {
dump('hasOneUpdated', $parent, $related);
});
}
}
use Chelout\RelationshipEvents\Concerns\HasMorphToManyEvents;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasMorphToManyEvents;
public static function boot()
{
parent::boot();
/**
* Many To Many Polymorphic Relations Events.
*/
static::morphToManyAttached(function ($relation, $parent, $ids, $attributes) {
dump('morphToManyAttached', $relation, $parent, $ids, $attributes);
});
static::morphToManyDetached(function ($relation, $parent, $ids) {
dump('morphToManyDetached', $relation, $parent, $ids);
});
}
public function tags()
{
return $this->morphToMany(Tag::class, 'taggable');
}
}
use Chelout\RelationshipEvents\Concerns\HasOneEvents;
use Chelout\RelationshipEvents\Traits\HasDispatchableEvents;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use HasDispatchableEvents;
use HasOneEvents;
protected $dispatchesEvents = [
'hasOneSaved' => HasOneSaved::class,
];
}
namespace App\Observer;
class UserObserver
{
/**
* Handle the User "hasOneCreating" event.
*
* @param \App\Models\User $user
* @param \Illuminate\Database\Eloquent\Model $related
*
* @return void
*/
public function hasOneCreating(User $user, Model $related)
{
Log::info("Creating profile for user {$related->name}.");
}
/**
* Handle the User "hasOneCreated" event.
*
* @param \App\Models\User $user
* @param \Illuminate\Database\Eloquent\Model $related
*
* @return void
*/
public function hasOneCreated(User $user, Model $related)
{
Log::info("Profile for user {$related->name} has been created.");
}
}
namespace App\Providers;
use App\Models\User;
use App\Observers\UserObserver;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
// ...
public function boot()
{
User::observe(UserObserver::class);
}
// ...
}
// ...
$user = factory(User::class)->create([
'name' => 'John Smith',
]);
// Create profile and assosiate it with user
// This will fire two events hasOneCreating, hasOneCreated
$user->profile()->create([
'phone' => '8-800-123-45-67',
'email' => '[email protected]',
'address' => 'One Infinite Loop Cupertino, CA 95014',
]);
// ...
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.