PHP code example of waad / laravel-dynamic-observer

1. Go to this page and download the library: Download waad/laravel-dynamic-observer 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/ */

    

waad / laravel-dynamic-observer example snippets




namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Waad\Observer\HasObserver;

class Work extends Model
{
    use HasObserver;  // ---> add this trait to connect with observer


    ......
}



namespace App\Models;

use App\Observers\MyWorkObserver;
use Illuminate\Database\Eloquent\Model;
use Waad\Observer\HasObserver;

class Work extends Model
{
    use HasObserver;

    // add this property to connect with observer custom name class
    public static $observer = MyWorkObserver::class;
}



namespace App\Models;

use App\Observers\MyWorkObserver;
use App\Observers\OurWorkObserver;
use Illuminate\Database\Eloquent\Model;
use Waad\Observer\HasObserver;

class Work extends Model
{
    use HasObserver;

    // add this property to connect with multi observer custom name class
    public static $observer = [MyWorkObserver::class, OurWorkObserver::class];
}



namespace App\Observers;

use App\Models\Work;

class WorkObserver
{
    
    public function creating(Work $work)
    {
        // This function is called when a new model instance is being created.
        $work->title = $work->title . ".....";
    }

    public function created(Work $work)
    {
        // This function is called after a new model instance is successfully 
        // created and saved to the database.

        $work->users()->attach([1,2]);
    }

    public function updating(Work $work)
    {
        // This function is called when an existing model instance is being updated.

        $work->status_color = $work->status ? 'green' : 'red';
    }

    public function updated(Work $work)
    {
        // This function is called after an existing model instance is successfully 
        // updated and saved to the database.

        $work->users()->sync([1,3]);
    }

    public function saving(Work $work)
    {
        // This function is called when a model instance is being saved
        // (either created or updated).

        $work->title = $work->title . ".....";
    }

    public function saved(Work $work)
    {
        // This function is called after a model instance is successfully saved 
        // (either created or updated).

        $work->status_string = 'working';
        $work->save();
    }

    public function deleting(Work $work)
    {
        // This function is called when an existing model instance is being deleted.

        $work->users()->detach();
    }

    public function deleted(Work $work)
    {
        // This function is called after an existing model instance is successfully deleted 
    }

    public function restoring(Work $work)
    {
        // This function is called when a "soft-deleted" model instance is being restored.
    }

    public function restored(Work $work)
    {
        // This function is called after a "soft-deleted" model instance is successfully restored.
    }

    public function retrieved(Work $work)
    {
        // This function is called after a model instance is retrieved from the database.

        $work->increment('views');
    }
}
bash
php artisan make:observer {YourModel}Observer --model={YourModel}