friendsofhyperf/model-scope

The model scope annotation for Hyperf.

v3.1.0 2024-03-20 00:42 UTC

This package is auto-updated.

Last update: 2024-04-20 00:56:40 UTC


README

Latest Stable Version Total Downloads License

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
{
    //
}

Contact

License

MIT