Logo

dev-resources.site

for different kinds of informations.

A Quick Intro To Eloquent Observers

Published at
4/22/2024
Categories
laravel
eloquent
observers
Author
mtownsend5512
Categories
3 categories in total
laravel
open
eloquent
open
observers
open
Author
13 person written this
mtownsend5512
open
A Quick Intro To Eloquent Observers

Laravel's Eloquent Observers are powerful tools that allow you to attach actions to model events that occur throughout your app. These events include creating, updating, deleting, and more, providing a convenient way to respond to changes in your application's data without having to scatter code all over your app's codebase.

Implementing observers involves creating a dedicated class for each model, usually within the app/Observers directory, and defining methods that correspond to the desired model events. This separation of concerns enhances code readability and makes it easier to manage application logic, especially as it grows in complexity.

Here is a list of all the Laravel model events you can listen for, utilizing Post as our example model:


namespace App\Observers;

use App\Models\Post;

class PostObserver
{
    public function retrieved(Post $post): void
    {
        // Logic to be executed just after a Post is retrieved from the DB
    }

    public function creating(Post $post): void
    {
        // Logic to be executed just before creating a new Post is being created for the first time
    }

    public function created(Post $post): void
    {
        // Logic to be executed after creating a new Post is created for the first time
    }

    public function updating(Post $post): void
    {
        // Logic to be executed just before updating a Post
    }

    public function updated(Post $post): void
    {
        // Logic to be executed after updating a Post
    }

    public function saving(Post $post): void
    {
        // Logic to be executed just before saving a Post (fires for both create and update)
    }

    public function saved(Post $post): void
    {
        // Logic to be executed after saving a Post (fires for both create and update)
    }

    public function deleting(Post $post): void
    {
        // Logic to be executed just before deleting a Post
    }

    public function deleted(Post $post): void
    {
        // Logic to be executed after deleting a Post
    }

    public function forceDeleting(Post $post): void
    {
        // Logic to be executed before force deleting a Post
    }

    public function forceDeleted(Post $post): void
    {
        // Logic to be executed after force deleting a Post
    }

    public function restoring(Post $post): void
    {
        // Logic to be executed just before restoring a soft-deleted Post
    }

    public function restored(Post $post): void
    {
        // Logic to be executed after restoring a soft-deleted Post
    }

    public function replicating(Post $post): void
    {
        // Logic to be executed after replicating a Post
    }
}
Enter fullscreen mode Exit fullscreen mode
eloquent Article's
30 articles in total
Favicon
Laravel Eloquent ORM in Bangla Part-3 (Models Retrieving)
Favicon
Prunable Eloquent Models
Favicon
Creating a Mini Blog API with Lithe and Eloquent
Favicon
Criando uma API de Mini Blog com Lithe e Eloquent
Favicon
Understanding Self-Relationships in Laravel Models: A Simple Guide
Favicon
Eloquent Trick: Laravel Model from Subquery
Favicon
Eloquent ORM: Accessor and Mutator
Favicon
Implementing User Suspension in Your Laravel Application
Favicon
Streamlining Eloquent Queries: Mastering User Scopes and Global Scopes in Laravel
Favicon
Getting data from resource responses before sending it to the client
Favicon
Including extra meta data with a resource response
Favicon
A Quick Intro To Eloquent Observers
Favicon
Customizing API Resource Pagination Structure
Favicon
What’s New in Laravel 11?
Favicon
Observing your integrity
Favicon
Laravel route binding for finite objects
Favicon
Simplify Slug Creation for Eloquent Models in Laravel
Favicon
Laravel: API Resources: With or Without "data"?
Favicon
Optimizing Laravel Eloquent queries
Favicon
Using Laravel push method to update your model and its relationships
Favicon
Laravel101: Exploring Entity-Model Relationships
Favicon
How to delete data from one to many relationship in Laravel?
Favicon
How to update a one-to-many relationship in Laravel?
Favicon
How can you retrieve data from a one-to-many relationship in Laravel?
Favicon
How to insert data in one to many relationship in database?
Favicon
How to create a One-To-Many relationship in Laravel?
Favicon
How to delete data from one to one relationship in Laravel?
Favicon
How to update a one-to-one relationship in Laravel?
Favicon
How can you retrieve data from a one-to-one relationship in Laravel?
Favicon
How to insert data in one to one relationship in database?

Featured ones: