【发布时间】:2020-12-23 18:11:40
【问题描述】:
我正在使用流明 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。但没有运气
【问题讨论】:
-
这个Article 解释了如何将 laravel Observers 带入 lumen。我使用 lumen 8.x 对其进行了测试,它可以工作。