Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Laravel News:
Getting Started with Laravel Model Events
Mar 16, 2018 @ 14:47:09

The Laravel News site has a tutorial posted that introduces you to model events in Eloquent models and how to use them effectively in your code.

Laravel Model events allow you to tap into various points in a model’s lifecycle, and can even prevent a save or delete from happening. The Laravel model events documentation outlines how you can hook into these events with event classes, but this article aims to build upon and fill in a few additional details on setting up events and listeners.

The article starts with an overview of the different types of events available on the models and a brief summary of when each are fired. It then walks you through the process of registering events and where to configure the event-to-handler relationship. The artisan make:event command can then generate the skeleton code for you. In this class you can then add the handler method and then configure it in the providers. Finally it shows an example of how to test the event, stopping the save event and using observers to group the event handling.

tagged: laravel eloquent model event tutorial introduction

Link: https://laravel-news.com/laravel-model-events-getting-started


Trending Topics: