friendsofhyperf / model-scope
The model scope annotation for Hyperf.
Fund package maintenance!
huangdijia
hdj.me/sponsors
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)
README
The model scope annotation for Hyperf.
Installation
composer require friendsofhyperf/model-scope
Usage
- Define a 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)); } }
- Binding on model
namespace App\Model; use App\Model\Scope\AncientScope; use FriendsOfHyperf\ModelScope\Annotation\ScopedBy; #[ScopedBy(AncientScope::class)] class User extends Model { // }