When to use Events of Laravel's Events and Observers

Source: Internet
Author: User

What is an event? If you search for this word in Google, you will get multiple results. For example, it is defined as something that has occurred or is considered to have occurred; an accident, especially a particularly important event. It can also be defined as a thing that occurs in a specific period of time.

I like these two definitions, because they are very consistent with our content. In fact, in a sense, you can regard this specific period as the model's lifecycle.


You can create a new instance, update an existing instance, or delete it. Each operation you can perform involves two events.

I just created a record, deleted the record, and I am updating the record. It sounds natural, right?

During the model lifecycle, when something happens, Eloquent triggers some events:

Creating
Created
Updating
Updated
Saving
Saved
Deleting
Deleted
Restoring
Restored
Each operation corresponds to two independent events. As you may imagine, they refer to separate moments. We have created an operation as an instance:

You have a creating event, which can be understood as "the creation operation is about to happen", and "created" indicates "the event has occurred ".

Scientists may say:

Creating: indicates the t-1 moment.
Created: it is related to the t + 1 moment
Therefore, the following three basic operations have two corresponding events: create, update, and delete ).

In addition, you can see two other operations: save and restore ). But don't worry. They are not complicated:

Save: you only need to know that the save operation is related to create and update. Let's assume that you need to add a behavior, whether the application creates a new record or updates an existing record. Do I have to declare the same thing twice? You only need a common save operation.
Restroe: the restore operation is used when a model uses soft delete and the undo operation is performed.
Well, I know what you are thinking: What is the deeper meaning of this concept? We use instances to answer this question.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.