# Observer

# Methods

retrieved, creating, created, updating, updated, saving, saved, deleting, deleted, restoring, restored, replicating

# Create Observer

php artisan make:observer ProductObserver --model=Product

# Register Observer

# Scenario 1

App\Providers\EventServiceProvider

use App\Models\Product;
use App\Observers\ProductObserver;

/**
 * Register any events for your application.
 *
 * @return void
 */
public function boot()
{
    Product::observe(ProductObserver::class);
}

# Scenario 2

// App/Providers/EventServiceProvider.php

use App\Models\User;
use App\Observers\UserObserver;

...   
   
protected $observers = [
    User::class => [UserObserver::class],
];

# Example #1

<?php

namespace App\Observers;

use App\Models\Product;

class ProductObserver
{
    /**
     * Handle the Product "show" event.
     *
     * @param  \App\Models\Product  $Product
     * @return void
     */
    public function retrieved(Product $product)
    {
        $product->increment('views');
    }

    /**
     * Handle the Product "created" event.
     *
     * @param  \App\Models\Product  $Product
     * @return void
     */
    public function created(Product $product)
    {
        Log::info('The '. $product->name .' product was created by ' . auth()->user()->name);
    }

    /**
     * Handle the Product "updated" event.
     *
     * @param  \App\Models\Product  $Product
     * @return void
     */
    public function updated(Product $product)
    {
        Log::info('The ' . $product->name .' product was updated by ' . auth()->user()->name);
    }

    /**
     * Handle the Product "deleted" event.
     *
     * @param  \App\Models\Product  $Product
     * @return void
     */
    public function deleted(Product $product)
    {
        Log::warning('The ' . $product->name .' was soft deleted by ' . auth()->user()->name);
    }
	
	/**
     * Handle the product "restored" event.
     *
     * @param  Product  $product
     * @return void
     */
    public function restored(Product $product)
    {
        Log::info('The ' . $product->name .' was restored by ' . auth()->user()->name);
    }

    /**
     * Handle the Product "forceDeleted" event.
     *
     * @param  \App\Models\Product  $product
     * @return void
     */
    public function forceDeleted(Product $product)
    {
        Log::warning('The ' . $product->name .' was force deleted by ' . auth()->user()->name);
    }
}
Last Updated: 12/11/2022, 22:04:16