friendsofhyperf/model-scope

Hyperf 的模型作用域注解。

v3.1.31 2024-06-17 01:41 UTC

This package is auto-updated.

Last update: 2024-09-25 00:30:22 UTC


README

Latest Stable Version Total Downloads License

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

联系

许可证

MIT