waad/laravel-dynamic-observer

Call observer of the model without requiring in any provider, support multi observers

v1.1.1 2025-01-25 21:15 UTC

This package is not auto-updated.

Last update: 2025-02-08 21:35:29 UTC


README

Call observer of the model from the direct model by trait HasObserver without requiring any provider, support multi observers. See

Documentation

๐Ÿ“‹ Requirements

  • PHP 7.3 or higher
  • Laravel 6.0 or higher

๐Ÿ’ผ Installation

Require this package with composer using the following command:

composer require waad/laravel-dynamic-observer

ย 

๐Ÿ’ฏ Usage

To properly use this package, follow the steps that meet your needs

  • ๐ŸŸข will connect dynamically with an observer named WorksObserver in App\Observers namespace
<?php

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


    ......
}

ย 

  • ๐ŸŸข if using custom observer different name class use $observer property
<?php

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;
}

ย 

  • ๐ŸŸข if using multi observer different names classes used $observer property
<?php

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];
}

ย 

๐Ÿ” Example Obsever

  • to create an observer use this command replace {YourModel} with your model name
php artisan make:observer {YourModel}Observer --model={YourModel}

ย  ย 

๐Ÿ”ฅ๐Ÿ”ฅ๐Ÿ”ฅ The following example shows all available observer methods. You can copy any needed methods to your generated observer file:

<?php

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');
    }
}

๐Ÿงช Testing

You can run the test suite using the following command:

composer test

๐Ÿš€ About Me

I'm a developer ...

โš–๏ธ License

The MIT License (MIT). Please see License File for more information.