Ejemplo 1: crear observador en laravel
php artisan make:observer nameOfObserver --model=modelName
Ejemplo 2: observador de laravel
php artisan make:observer UserObserver --model=User
Ejemplo 3: modelo observador laravel
namespace App;
use IlluminateDatabaseEloquentModel;
class Post extends Model
{
protected $table = 'posts';
protected $fillable = ['title', 'slug', 'content'];
protected static function boot()
{
parent::boot();
static::saving(function ($model) {
$model->slug = str_slug($model->title);
});
}
}
Ejemplo 4: observador de laravel
<?php
namespace AppObservers;
use AppModelsUser;
class UserObserver
{
/**
* Handle the User "created" event.
*
* @param AppModelsUser $user
* @return void
*/
public function created(User $user)
{
//
}
/**
* Handle the User "updated" event.
*
* @param AppModelsUser $user
* @return void
*/
public function updated(User $user)
{
//
}
/**
* Handle the User "deleted" event.
*
* @param AppModelsUser $user
* @return void
*/
public function deleted(User $user)
{
//
}
/**
* Handle the User "forceDeleted" event.
*
* @param AppModelsUser $user
* @return void
*/
public function forceDeleted(User $user)
{
//
}
}
Ejemplo 5: observador de laravel
<?php
namespace AppProviders;
use AppObserversUserObserver;
use AppModelsUser;
use IlluminateSupportServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
User::observe(UserObserver::class);
}
}
¡Haz clic para puntuar esta entrada!
(Votos: 0 Promedio: 0)