【发布时间】:2021-12-05 08:04:53
【问题描述】:
我有两个文件 userPolicy.php 和 userObserver.php ,我在这两个文件中编写了一些逻辑,这意味着如果用户有任何活动订阅,则用户不会被删除到此为止,它工作正常。
userPolicy.php
public function delete(User $user, $item)
{
$canceled_subscription=new userObserver();
return (!$canceled_subscription->deleting($item)) && ($user->hasAdminRole());
}
userObserver.php
public function deleting(Plan $item){
//based on $has_subscriptions it should allow to delete or call the userPolicy.php delete function
$has_subscriptions=$item->subscriptions()->where('status','!=','canceled')->exists();
return $has_subscriptions;
}
现在我想要的是我想重构 userPolicy.php 中的代码,这意味着有没有办法减少 userPolicy.php 文件中的代码(主要是 $canceled_subscriptions),有没有机会减少代码来自 userPolicy.php 请帮我重构代码
【问题讨论】:
标签: php laravel observers laravel-events