Getting Started with Laravel 6 Model Events

Getting Started with Laravel 6 Model Events

In this tutorial, we'll learn about model events in Laravel 6 for using them in our CRM application to update the role of the user to an admin before saving it to the database.

Originally published at https://www.techiediaries.com

The Laravel ORM provides powerful abstractions for working with the supported databases. One of the features of Eloquent is the implementation of the observer pattern for sending and listening to events sent by Laravel models when actions such as creating or saving models are executed.

If you are not familiar with the observer pattern, it's simply:

A software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. You can find more information on Wikipedia.

Design patterns are simply common solutions to problems in software development, that are well tested and tried by developers.

Laravel Model Events

A Model in Laravel provides an abstraction for working with a database table with a high-level API. Among these APIs, are events which are fired when actions are performed on the model.

Models events are simpy hooks into the important points of a model's lifecycle which you can use to easily run code when database records are saved, updated or deleted.

Events receive the instance of the model which is being saved, updated or deleted.

These are the events that you can use with your Laravel models:

  • creating and created: sent before and after records have been created.
  • updating and updated: sent before and after records are updated.
  • saving and saved: sent before and after records are saved (i.e created or updated).
  • deleting and deleted: sent before and after records are deleted or soft-deleted.
  • restoring and restored: sent before and after soft-deleted records are restored.
  • retrieved: sent after records have been retrieved.

According to the official website:

The retrieved event will fire when an existing model is retrieved from the database. When a new model is saved for the first time, the creating and created events will fire. If a model already existed in the database and the save method is called, the updating / updated events will fire. However, in both cases, the saving / saved events will fire.

Now, let's listen for the saving event of the User model and update the role field to admin before saving the first record. Open the app/User.php file and update as follows:

<?php

namespace App;

use Illuminate\Notifications\Notifiable; use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable {    use Notifiable;

   protected $fillable = [        'name', 'email', 'password',    ];

   protected $hidden = [        'password', 'remember_token',    ];

   protected $casts = [        'email_verified_at' => 'datetime',    ];

   protected static function boot()    {        parent::boot();        User::saving(function ($model) {            if(!User::where("role","=", "admin")->exists())            {                $model->role = 'admin';            }

       });    } }

We simply added a static boot() method to our User model. Inside this method, we called the saving() method and we passed a closure function that receives the instance of the User model which is being saved.

Inside the saving() listener function, we check if a user with an admin role exists in the database, if not we assign the admin value to the role field.

Now, start your Laravel app if it's not running yet:

$ php artisan serve 

Head to the http://localhost:8000 address in your web browser, you should be able to register for a first account with an admin role. After that, the registration will be disabled.

Check out this page of the official docs for another approach of listening to model events using the $dispatchesEvents property on your Eloquent model and maps various points of the model's lifecycle to your own event classes:

At this point of our tutorial, we have seen how we can listen to the saving event in our application. While this is enough for implementing the required functionality but let's see how we can use a model observer to do the same.

Conclusion

In this tutorial, we've learned about Laravel Model events and we have seen how to listen for the saving event on the User model for updating the role of the user being saved. 

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow me on Facebook | Twitter

Further reading

Laravel 5.8 Tutorial for Beginners

What's New in Laravel 6.0

Laravel 6 tutorial – Build Your First CRUD App with Example

Laravel 6 CRUD Application Tutorial





laravel php

What's new in Bootstrap 5 and when Bootstrap 5 release date?

How to Build Progressive Web Apps (PWA) using Angular 9

What is new features in Javascript ES2020 ECMAScript 2020

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Random Password Generator Online

HTML Color Picker online | HEX Color Picker | RGB Color Picker

Laravel Sanctum vs Passport

After the Laravel Sanctum package was released? Everywhere question was asked that When to use Sanctum and when to use Passport? So, in this video, you will ...

Clear Cache in Laravel 6.8 App using Artisan Command Interface (CLI)

In Laravel 6 tutorial, we learn how to use PHP artisan command interface (CLI) to clear the cache from Laravel 6.8 application. How To Clear Cache in Laravel 6.8 Application using Artisan Command Line Interface (CLI)? How to clear route cache using php artisan command? How to easily clear cache in Laravel application? How to clear config cache in PHP Laravel via artisan command? How to clear Laravel view cache? How to Reoptimized class in Laravel via artisan CLI?

Hire Dedicated PHP Developer

Looking to hire affordable yet experienced PHP developers? **[Hire Dedicated PHP Developer](https://hourlydeveloper.io/hire-dedicated-php-developer/ "Hire Dedicated PHP Developer")**, who can convert your idea to reality, within the stipulated...

Get Weather Data with Laravel Weather

Get Weather Data with Laravel Weather. Laravel Weather is a good package which we can use to get weather data. It's a wrapper around Open Weather Map API (Current weather). A wrapper around Open Weather Map API (Current weather)

Laravel PHP Framework Tutorial - Laravel Full Course (2020)

Learn Laravel PHP framework from very basics to advanced using Laravel Livewire by creating advanced todo list project in this full course in 6.5 hours. Laravel PHP Framework Tutorial - Laravel Full Course (2020)