friendsofhyperf/model-scope

The model scope annotation for Hyperf.

v3.1.48 2024-11-28 08:49 UTC

This package is auto-updated.

Last update: 2024-12-04 02:42:13 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