Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mobiosolutions/laravel-observer-example
A example code for laravel model observer.
https://github.com/mobiosolutions/laravel-observer-example
Last synced: 2 days ago
JSON representation
A example code for laravel model observer.
- Host: GitHub
- URL: https://github.com/mobiosolutions/laravel-observer-example
- Owner: mobiosolutions
- Created: 2020-01-16T06:31:18.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-02-02T10:21:28.000Z (almost 2 years ago)
- Last Synced: 2023-03-02T04:41:00.044Z (over 1 year ago)
- Language: PHP
- Size: 190 KB
- Stars: 0
- Watchers: 1
- Forks: 1
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# 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/