流明观察者不在雄辩模型上工作

问题描述 投票:0回答:1

我正在使用流明5.5。我试图让观察者在更新/删除模型时被调用。当我尝试使用用户模型时,观察者未调用。当我通过事件进行操作时,一切正常。它甚至没有显示任何错误。

这是我的代码:

AppServiceProvider.php

....
use App\Models\User;
use App\Observers\UserObserver;
...

public function boot() {
    User::observe(UserObserver::class);

}

App \ Models \ User.php

...
    public static function changeCustomerStatus(int $customerID): int{
        $customer = self::where([
                'id'        => $customerID,
                'user_type' => app('config')->get('user_type.CUSTOMER')
            ])
            ->first();

        if ($customer) {
            $customer->status = $customer->status == app('config')->get('status.ACTIVE') ? app('config')->get('status.DEACTIVE') : app('config')->get('status.ACTIVE');

            if ($customer->save()) {
                return $customer->status;
            }

            return 0;
        }   
        else 
            return 0;
    }
...

App \ Observers \ UserObserver.php

<?php

namespace App\Observers;

use App\Models\User;

class UserObserver {

    public function updated(User $user) {
        if ($user->status === app('config')->get('status.DEACTIVE')) {
            app('log')->info('updated');
        }
    }


public function saved(User $user) {
        if ($user->status === app('config')->get('status.DEACTIVE')) {
            app('log')->info('saved');
        }
    }


    public function deleted(User $user) {
        app('log')->info('deleted');
    }
}

我什至都做了composer dump-autoload。但没有运气

laravel lumen
1个回答
0
投票

流明没有观察功能。您可以改用“事件”,也可以创建自定义观察者并从代码中调用其函数。

在此处阅读文档-Events

© www.soinside.com 2019 - 2024. All rights reserved.