Laravel Model Observers

 37
Laravel Model Observers

1. How to use Laravel Observer?
 The model events observed and used across the Laravel model have the following.

  • Retrieved: After a record has been retrieved.
  • Creating: Before a record has been created.
  • Created:  After a record has been created
  • Updating: Before a record has been updated.
  • Updated:  After a record has been updated.
  • Saving: Before a record is saved (Either created or Updated).
  • Saved: After a record has been saved (Either created or Updated).
  • Deleting: Before a record is deleted or soft-deleted.
  • Deleted: After a record has been deleted or soft-deleted.
  • Restoring: Before a soft-deleted record is going to be restored.
  • Restored: After a soft-deleted record has been restored.

Step 1: Create a model and Migration File:
 For example, we create a Product model for the observer. 

php artisan make:model Product -m

The product migration file will be created in the app\database\migrations directory. Below is the migration file::

Schema::create('products', function (Blueprint $table) {
$table->id();
    $table->string('name');
    $table->string('price');
    $table->string('slug')->nullable();
    $table->string('unique_id')->nullable();
    $table->timestamps();
});

To migrate the products table, run the following command.

php artisan migrate

The Product model is located in app\models directory and include fillable fields. Below is an example of the Product model code:

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
    use HasFactory;
    protected $fillable = [
        'name', 'price', 'slug', 'unique_id'
    ];
}

Step 2: Create an Observer Class for the Product :
To create an Observer class for the Product model,  Run the following command:

php artisan make:observer ProductObserver --model=Product


The ProductObserver class will be created in the app/Observers directory. Below is a basic example of how to use the observer:

<?php
namespace App\Observers;

use App\Models\Product;
use Illuminate\Support\Str;

class ProductObserver
{
    public function creating(Product $product)
    {
        $product->slug = Str::slug($product->name);
    }

    /**
     * Handle the Product "created" event.
     */
    public function created(Product $product): void
    {
        $product->unique_id = 'PR-' . $product->id;
        $product->save();
    }
}


Step 3: Create a Product Controller:
To create a Product controller, Run the following command:

php artisan make:controller ProductController


The Product controller will be created in the app/Http/Controllers directory. Below is a basic example:

<?php
namespace App\Http\Controllers;

use App\Models\Product;
use Illuminate\Http\Request;

class ProductController extends Controller
{
    public function index()
    {
        $product = Product::create([
        'name' => 'Platinum 1',
        'price' => 10
        ]);
    }
}

Post a Comment

0Comments

* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Post a Comment (0)