Events and observers of Laravel (I.)

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 significant one. It can also be defined as something that happens at a specific point in a given period of time.

I like these two definitions because they fit well with our content. In fact, in a sense, you can think of this particular period as the life cycle of the model.

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

On the basis of this: I just created a record, I deleted the record, I'm updating that record, it sounds natural, right?

In the life cycle of a model, when something happens, eloquent triggers some events:

    • Creating
    • Created
    • Updating
    • Updated
    • Saving
    • Saved
    • deleting
    • Deleted
    • Restoring
    • Restored

For each operation, it corresponds to two separate events. As you might imagine, they refer to a single moment. We have created the operation as an instance:

You have a creating event that can be understood as "the creation operation is about to occur" and created means "the event has already occurred."

Scientists may say:

    • Creating: Is the t–1 moment
    • Created: is associated with the T + 1 moment

So, for the following three basic operations, there are two corresponding events: Create, update, and delete.

In addition, you can see two other actions: Save and Recovery (restore). But, don't worry, they're not complicated:

    • Save: You just need to know that the save operation is related to create and update. Let's say you need to add a behavior where the application creates a new record or updates an existing one. Does it have to be declared two times for the same thing? Only one normal save operation is required.
    • Restroe: The restore operation is used when one of your models uses a soft delete and performs a undo operation.

Well, I know what you're thinking: What's the meaning of the deeper layer of the concept? We use examples to answer.

  • 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.