friendsofhyperf /model-scope
Hyperf 的模型作用域注解。
赞助包维护!
huangdijia
hdj.me/sponsors
v3.1.31
2024-06-17 01:41 UTC
Requires
- hyperf/di: ~3.1.0
- hyperf/event: ~3.1.0
- hyperf/stdlib: ~3.1.0
Suggests
- hyperf/database: Required to use (~3.1.0)
This package is auto-updated.
Last update: 2024-09-25 00:30:22 UTC
README
Hyperf 的模型作用域注解。
安装
composer require friendsofhyperf/model-scope
用法
- 定义作用域
namespace App\Model\Scope; use Hyperf\Database\Model\Builder; use Hyperf\Database\Model\Model; use Hyperf\Database\Model\Scope; class AncientScope implements Scope { /** * Apply the scope to a given Model query builder. */ public function apply(Builder $builder, Model $model): void { $builder->where('created_at', '<', now()->subYears(2000)); } }
- 模型绑定
namespace App\Model; use App\Model\Scope\AncientScope; use FriendsOfHyperf\ModelScope\Annotation\ScopedBy; #[ScopedBy(AncientScope::class)] class User extends Model { // }