承接 josrom/eloquent-crud-repository 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

josrom/eloquent-crud-repository

Composer 安装命令:

composer require josrom/eloquent-crud-repository

包简介

All the basic methods to any Laravel project with Repository pattern

README 文档

README

CircleCI Latest Stable Version Total Downloads License

Introduction

EloquentCrudRepository provides a well tested and complete base to create more model repositories using the repository pattern with Eloquent as ORM.

Public methods available

Name Parameters Return
all array $with = [] \Illuminate\Database\Eloquent\Collection
allWithTrashed array $with = [] \Illuminate\Database\Eloquent\Collection
allTrashed array $with = [] \Illuminate\Database\Eloquent\Collection
find int $id, array $with = [] \Illuminate\Database\Eloquent\Model
findWithTrashed int $id, array $with = [] \Illuminate\Database\Eloquent\Model
findTrashed int $id, array $with = [] \Illuminate\Database\Eloquent\Model
findBy string $field, mixed $value, string $comparison = '=', bool $strict = true, array $with = [] \Illuminate\Database\Eloquent\Model
findByWithTrashed string $field, mixed $value, string $comparison = '=', bool $strict = true, array $with = [] \Illuminate\Database\Eloquent\Model
findByTrashed string $field, mixed $value, string $comparison = '=', bool $strict = true, array $with = [] \Illuminate\Database\Eloquent\Model
newModel array $params = [] \Illuminate\Database\Eloquent\Model
create array $params \Illuminate\Database\Eloquent\Model
update int $id, array $params \Illuminate\Database\Eloquent\Model
delete int $id bool
forceDelete int $id bool
restore int $id bool
paginate \Illuminate\Database\Eloquent\Builder $query, int $page = 0, int $limit = 15 object
paginateCollection \Illuminate\Database\Eloquent\Collection $collection, int $page = 0, int $limit = 15 object
pagination int $page = 0, int $limit = 15 object
paginationWithTrashed int $page = 0, int $limit = 15 object
paginationOnlyTrashed int $page = 0, int $limit = 15 object

Protected methods available

Name Parameters Return
checkCanShow ?\Illuminate\Database\Eloquent\Model $model void
checkCanCreate array $params void
checkCanUpdate \Illuminate\Database\Eloquent\Model $model, array $newValues void
checkCanDelete \Illuminate\Database\Eloquent\Model $model void
checkCanRestore \Illuminate\Database\Eloquent\Model $model void
canShow ?\Illuminate\Database\Eloquent\Model $model bool
canCreate array $params bool
canUpdate \Illuminate\Database\Eloquent\Model $model, array $newValues bool
canDelete \Illuminate\Database\Eloquent\Model $model bool
canRestore \Illuminate\Database\Eloquent\Model $model bool
hasSoftDeletes bool

Usage

Basic repository

Extend CrudRepository and inject your Eloquent model. Annotate with @extends CrudRepository<YourModel> so that IDEs and static analysis tools (PHPStan, Psalm) can infer the concrete model type on every return value:

use Eloquent\Crud\Repository\Eloquent\CrudRepository;

/**
 * @extends CrudRepository<User>
 */
class UserRepository extends CrudRepository
{
    public function __construct()
    {
        parent::__construct(new User());
    }
}

With that annotation in place, find(), create(), update(), all(), etc. all resolve to User (or Collection<int, User>) in your IDE instead of the base Model.

Access control

Override any can* method to restrict access. The corresponding checkCan* guard calls it and throws AccessDeniedException (HTTP 403) on denial:

/**
 * @extends CrudRepository<Post>
 */
class PostRepository extends CrudRepository
{
    public function __construct()
    {
        parent::__construct(new Post());
    }

    protected function canUpdate(Model $model, array $newValues): bool
    {
        return Auth::id() === $model->user_id;
    }

    protected function canDelete(Model $model): bool
    {
        return Auth::user()->isAdmin();
    }
}

SoftDeletes

If your model uses the SoftDeletes trait, the WithTrashed and OnlyTrashed variants are automatically available:

/**
 * @extends CrudRepository<Post>
 */
class PostRepository extends CrudRepository
{
    public function __construct()
    {
        parent::__construct(new Post()); // Post uses SoftDeletes
    }
}

$repo->allWithTrashed();          // active + soft-deleted
$repo->allTrashed();              // only soft-deleted
$repo->findWithTrashed($id);
$repo->findTrashed($id);
$repo->forceDelete($id);
$repo->restore($id);

Installation

To get the last version of EloquentCrudRepository, simply require the project using Composer:

composer require josrom/eloquent-crud-repository

Instead, you may of course manually update your require block and run composer update if you so choose:

{
    "require": {
        "josrom/eloquent-crud-repository": "^13.0"
    }
}

License

EloquentCrudRepository is open-sourced software licensed under the MIT license

统计信息

  • 总下载量: 2.37k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2018-02-06

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固