prettus/l5-repository筆記

caomaoboy發表於2021-07-30

單詞必備

  • repository 儲存庫
  • Contracts 合同(==契約==)
  • criteria ==標準==;(評判或作決定的)準則;原則

1.安裝

1.1.Composer

執行以下命令以獲取包的最新版本:
composer require prettus/l5-repository

1.2.Laravel

1.2.1.>= laravel5.5

ServiceProvider將自動附加

1.2.2.其他

  • config/app.php中,將Prettus\Repository\Providers\RepositoryServiceProvider::class新增到Providers陣列的末尾:
    'providers' => [
    ...
    Prettus\Repository\Providers\RepositoryServiceProvider::class,
    ],
  • Lumen
    $app->register(Prettus\Repository\Providers\LumenRepositoryServiceProvider::class);
  • 釋出配置
    php artisan vendor:publish --provider "Prettus\Repository\Providers\RepositoryServiceProvider"

2.方法

2.1.Prettus\Repository\Contracts\RepositoryInterface

  • all($columns = array(‘*’))

  • first($columns = array(‘*’))

  • paginate($limit = null, $columns = [‘*’])

  • find($id, $columns = [‘*’])

  • findByField($field, $value, $columns = [‘*’])

  • findWhere(array $where, $columns = [‘*’])

  • findWhereIn($field, array $where, $columns = [*])

  • findWhereNotIn($field, array $where, $columns = [*])

  • findWhereBetween($field, array $where, $columns = [*])

  • create(array $attributes)

  • update(array $attributes, $id)

  • updateOrCreate(array $attributes, array $values = [])

  • delete($id)

  • deleteWhere(array $where)

  • orderBy($column, $direction = ‘asc’);

  • with(array $relations);

  • has(string $relation);

  • whereHas(string $relation, closure $closure);

  • hidden(array $fields);

  • visible(array $fields);

  • scopeQuery(Closure $scope);

  • getFieldsSearchable();

  • setPresenter($presenter);

  • skipPresenter($status = true);

    2.2.Prettus\Repository\Contracts\RepositoryCriteriaInterface

  • pushCriteria($criteria)

  • popCriteria($criteria)

  • getCriteria()

  • getByCriteria(CriteriaInterface $criteria)

  • skipCriteria($status = true)

  • getFieldsSearchable()

2.3.Prettus\Repository\Contracts\CacheableInterface

  • setCacheRepository(CacheRepository $repository)
  • getCacheRepository()
  • getCacheKey($method, $args = null)
  • getCacheMinutes()
  • skipCache($status = true)

2.4.Prettus\Repository\Contracts\PresenterInterface

  • present($data);

2.5.Prettus\Repository\Contracts\Presentable

  • setPresenter(PresenterInterface $presenter);
  • presenter();

2.6.Prettus\Repository\Contracts\CriteriaInterface

  • apply($model, RepositoryInterface $repository);

2.7.Prettus\Repository\Contracts\Transformable

  • transform();
本作品採用《CC 協議》,轉載必須註明作者和本文連結