口才配置_快速提示:口才观察者的便捷魔力

口才配置

If you’ve used Eloquent on medium to large projects before, you may have encountered a situation where you want to take action when something happens to your models. Eloquent provides a convenient way to do so.

如果您以前在大中型项目上使用过Eloquent,则可能会遇到一种情况,当模型发生问题时,您想采取行动。 口才提供了一种方便的方法。

Laravel Logo

观察者模式 (The Observer Pattern)

The observer pattern is 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. – Wikipedia

观察者模式是一种软件设计模式,其中一个称为主题的对象会维护其依赖项的列表(称为观察者),并通常通过调用其方法之一来自动将状态更改通知他们。 –维基百科

In our case, Eloquent models can notify us about changes on a given model.

就我们而言,雄辩的模型可以通知我们有关给定模型的更改。

模型事件 (Model Events)

Eloquent provides a handful of useful events to monitor the model state: creating, created, updating, updated, deleting, deleted, saving, saved, restoring, restored.

Eloquent提供了一些有用的事件来监视模型状态: creatingcreatedupdatingupdateddeletingdeletedsavingsavedrestoringrestored

Notice the “ing/ed” difference.

注意“ ing / ed”的区别。

  • creating: Called before saving the new member.

    creating :在保存新成员之前调用。

  • created: Called after saving the member.

    created :保存成员后调用。

Eloquent also fires similar events that we can listen for. The below example attaches a listener to the creating event on the Member model.

口才还触发了我们可以听的类似事件。 下面的示例将侦听器附加到Member模型上的creating事件。

Event::listen("eloquent.created: App\\Member", function(Member $member) {
    // do something
});

创建观察者 (Creating Observers)

Let’s start by creating a new class under the App\Observers namespace and start defining our methods.

让我们首先在App\Observers命名空间下创建一个新类,然后开始定义我们的方法。

// app\Observers\MemberObserver.php

namespace App\Observers;

use App\Member;

class MemberObserver
{
    public function deleting(Member $member) {
        // do something
    }
}

We can use the event name as the method name for each one. We don’t have to define all the methods, just the ones that we want to use.

我们可以将事件名称用作每个方法的名称。 我们不必定义所有方法,只需定义我们要使用的方法即可。

Every member can subscribe to multiple services, and every service contains many members. Let’s assume we don’t have cascade deletion set up for the associated members_services table, and we need to delete associated services on member deletion to avoid errors when accessing subscribed members for a service.

每个成员可以订阅多个服务,每个服务包含许多成员。 假设我们没有为关联的members_services表设置级联删除,并且我们需要在成员删除时删除关联的服务,以避免访问服务的已订阅成员时出错。

// app\Observers\MemberObserver.php

namespace App\Observers;

use App\Member;

class MemberObserver
{
    public function deleting(Member $member) {
        $member->services()->delete();
    }
}

Now, the last thing is to attach this observer to the appropriate model. We can do this anywhere we want, but the practical place to put it is inside the app\Providers\AppProvider.php file inside the boot method.

现在,最后一件事是将该观察者附加到适当的模型上。 我们可以在任何需要的地方进行此操作,但实际放置位置在启动方法中的app\Providers\AppProvider.php文件中。

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        Member::observe("App\\Observers\\MemberObserver");
    }
}

I know that the cascade deletion example was simple and could be done in the controller or directly via MySQL, but this is just a proof of concept.

我知道级联删除示例很简单,可以在控制器中完成,也可以直接通过MySQL完成,但这只是概念上的证明。

The good thing about Eloquent observers is that we can abort the current action by returning a false value from the callback method:

Eloquent观察者的好处是,我们可以通过从回调方法返回一个假值来中止当前操作:

class MemberObserver
{
    public function deleting(Member $member) {
        $member->deleted_at = Carbon::now();
        $member->save();

        return false;
    }
}

In the example above, we are soft deleting members and returning false to abort the actual delete action.

在上面的示例中,我们是软删除成员,并返回false以终止实际的删除操作。



Eloquent has many hidden features, and this is one of them. You’ll see this used heavily in big applications and CMSes. If you have any questions or comments about Eloquent, be sure to post them below!

口才有很多隐藏的功能,这就是其中之一。 您将看到它在大型应用程序和CMS中大量使用。 如果您对Eloquent有任何疑问或意见,请务必在下面发布!

翻译自: https://www.sitepoint.com/quick-tip-the-convenient-magic-of-eloquent-observers/

口才配置

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值