friendsofhyperf/model-scope

The model scope annotation for Hyperf.

v3.1.41 2024-09-25 00:28 UTC

This package is auto-updated.

Last update: 2024-10-25 04:00:21 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