Laravel Event Model – one of the things that changes the way you code

Tram Ho

Hello everyone, this is my first time writing an article so there is nothing missing that everyone missed.

1. Get to the point

Surely many of you working with laravel have heard the phrase Event but it is quite confusing, this article will not go into the Event but I will introduce you. Event Model will give you another way of looking at events and also help your code look cleaner hihi.

Here I will mention eloquent to let you understand 1 most easily. For example, now you want to perform an action before creating or updating a table in the database. Actually here I have 2 tables

Worktime and Workmonth, to create the first Worktime, you must check to see if that Workmonth already exists, if so, continue to create Worktime and can get the Workmonth’s id. Conversely, if Workmonth does not have, you have to create Workmonth to continue. Normally, depending on each person, there will be ways to write on the model or controller, usually I see people using eloquent, they will usually write all code outside the controller, such as this:

This is the old code, but now I will introduce you to events in the model

You may already know by looking at this. I will say focus on creating and creating, the ones below it are similar and also quite a lot of functions, here I just introduce the four functions that I use most often. The first is creating, you just understand that is the action that happened before it created a new record. Many of you will wonder what the $ model here is, I said simply for you to understand it could be wrong, $ model contains the values ​​that you send to create a new record, you see the wallet section. The example below will understand. Next What is Created? Created is the action that after you have finished creating a new record, the $ model here will contain the values ​​that you saved.

Continuing the code above, I will show you how to use the Event Model so that you can understand it.

Many of you will be wondering here the missing data that has been created … will cause an error, the answer is NO, let’s continue to understand what the problem is.

Here I present the code like this, first I still check Workmonth as usual, but there will be $ model-> user_id is the variable that I passed in the controller. Next there is the $ model-> workmonth_id segment that I have assigned the workmonth_id to the model with a certain value before performing the save action in the database. The update I will not mention anymore, it’s just the same as in create only, you can manipulate the same but the action will be different.

When using the event model you saw my code in the controller is quite clean, I also looked through but did not see anyone writing about this problem so now I want to write, maybe will help you in 1 number of cases.

My post paused here, thank you for watching.

I am still a chicken code, so you guys should kick a little 😃 ).

Share the news now

Source : Viblo