Code Monkey home page Code Monkey logo

laravel-observer-example's Introduction

Laravel Model Observers

What is Laravel Model Observers

Laravel Model Observers are used to group event listeners for a model. Observers classes method names refer to the Eloquent event you want to listen for. These methods receive the model as their only argument. Laravel does not include a default directory for observers. Eloquent models fire several events, allowing you to hook into the following points in a model's lifecycle:

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 is 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.

Why We Want To Use Model Observer.

Now, suppose, if we want to concatenate the string with name or We have to add some tax to the product price, but we do not want to write the logic or function in the controller then, we can use the Model Events. It will fire automatically when the new record is created or updated or deleted. There are Some types of Model Events available in Laravel Doc

How To Create And Use Model Observer

Create Service Provider for the Observers. Make the service provider for the following command.

php artisan make:provider CategoryModelServiceProvider

Register this service provider into the Laravel App. Go to the config/app.php file. Add the following code.

'providers' => [
    App\Providers\CategoryModelServiceProvider::class,
 ]

Now run the following command it will create App/Observers/CategoryObserver file.

php artisan make:observer CategoryObserver

Then write function of observer like this.

public function creating(Category $category)
{
   Log::info("========= Observer Creating ==========");
}

Need to register this observer inside the App/Providers/CategoryModelServiceProvider.php file.

public function boot()
{
   Category::observe(CategoryObserver::class);
}

Installation and use

$ git clone https://github.com/mobiosolutions/laravel-observer-example.git

$ cp .env.example .env

Change configuration according to your need in ".env" file and create Database

$ composer install

$ php artisan migrate

$ php artisan serve

More From Our MobioSolutions Team

Read our Blog https://mobiosolutions.com/how-to-install-and-use-laravel-model-observers/

laravel-observer-example's People

Contributors

jigard avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

Forkers

leesbian

laravel-observer-example's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.