定制 lerouse/laravel-repository 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

lerouse/laravel-repository

Composer 安装命令:

composer require lerouse/laravel-repository

包简介

Repository implementation for Laravel.

README 文档

README

Laravel Repository Build Latest Stable Version License Innerbichler UK

Model Repository implementation for the Laravel framework ^v6.0 and above.

This package provides a structured framework to begin implementing a simple Model Repository structure in an existing Laravel application.

Installation

The recommended method to install LaravelRepository is with composer

php composer require lerouse/laravel-repository

Laravel without auto-discovery

If you don't use auto-discovery, add the ServiceProvider to the providers array in config/app.php

\Lerouse\LaravelRepository\LaravelRepositoryServiceProvider::class,

Package configuration

Copy the package configuration to your local config directory.

php artisan vendor:publish --tag=repository-config

Usage

Model Repository Location

By default Laravel Model Repository presumes that model repositories are stored in the directory app/repositories/model. If you would like to change the directory your model repositories are stored change the namespace variable in the config/repostiory.php config file.

'namespace' => '\App\Repositories\Model',

For example if your model repositories are located in app/model/repositories update the namespace to

'namespace' => '\App\Model\Repositories',

Creating a Repository

Repositories should extend the Lerouse\LaravelRepository\EloquentRepository method and implement the builder method which should return an instance of a Illuminate\Database\Eloquent\Builder.

<?php

namespace App\Repositories\Models;

use Illuminate\Database\Eloquent\Builder;
use Lerouse\LaravelRepository\EloquentRepository;
use App\Models\User;

class UserRepository extends EloquentRepository
{

   /**
    * Get the Repository Query Builder
    *
    * @return Builder
    */
   public function builder(): Builder
   {
       return User::query();
   }

}

The following methods are available out of the box:-

Method Return Type Description
all Collection Get all Models
paginate LengthAwarePaginator Get all models as Paginated Results
find Model Find a Model by its Primary Key
findMany Collection Find a collection of Models by their Primary Keys
create Model Create a new Model
createMany Boolean Create a collection of new Models
update Model Update a Model by its Primary Key
updateMany Boolean Update a collection of Models
delete Model/null Delete a Model by its Primary Key
deleteMany void Delete a collection of Models by their Primary Keys

Using a Repository

Repositories can be used directly (see above for available built in methods).

<?php

namespace App\Http\Controllers;

use Illuminate\Database\Eloquent\Builder;

class UserController
{

    /**
     * Get the Repository Query Builder
     *
     * @return Builder
     */
    public function index(): Builder
    {
        $repository = new UserRepository;

        return $repository->all(); 
    }

}

Extending a Repository

Repositories should be the single point for extending/amending queries to your models. The advantage of this is all of your model business logic is in a single location. See below a repository that contains commonly used extended functionality as well as some custom methods that are also used.

<?php

namespace App\Repositories\Models;

use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder;
use Lerouse\LaravelRepository\EloquentRepository;
use Illuminate\Database\Eloquent\Collection;
use App\Models\Team;
use App\Models\User;

class UserRepository extends EloquentRepository
{
   
   /**
    * Get all models as Paginated Results
    *
    * @param int|null $limit
    * @param int $page
    * @return LengthAwarePaginator
    */
   public function paginate(int $limit = null, int $page = 1): LengthAwarePaginator
   {
       return $this->builder()
                   ->orderBy($this->getModelTable() . '.created_at', 'desc')
                   ->paginate($limit ?? 25, $page);
   }

   /**
    * Get active Users
    *
    * @return Collection
    */
   public function active(): Collection
   {
       return $this->builder()
                   ->where($this->getModelTable() . 'active', '=', true)
                   ->orderBy($this->getModelTable() . '.created_at', 'desc')
                   ->get();
   }

   /**
    * Get the Repository Query Builder
    *
    * @return Builder
    */
   public function builder(): Builder
   {
       return User::query()
                   ->select($this->getModelTable() . '.*')
                   ->selectRaw('teams.name as user_team_name')
                   ->leftJoin('teams', 'teams.id', '=', $this->getModelTable() . '.team_id');
   }

}

Laravel Route Model Binding

Laravel route model binding provides a convenient way to automatically inject the model instances directly into your routes. To force the route model binding to use your repository rather than the model itself you can add the Lerouse\LaravelRepository\HasRepository trait to your model.

<?php

namespace App\Http\Controllers;

use Illuminate\Database\Eloquent\Model;
use Lerouse\LaravelRepository\HasRepository;

class User extends Model
{
   use HasRepository;
}

Now when you utilise Laravel's Route Model Binding, the find method on the repository will be used.

<?php

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Database\Eloquent\Builder;

class UserController extends Controller
{

    /**
     * Get the Repository Query Builder
     *
     * @return Builder
     */
    public function show(User $user): Builder // binding resolved through the UserRepository's find method 
    {
        return $user; 
    }

}

Please Note: The repositories location is determined via the namespace variable in the config/repostiory.php config file.

'namespace' => '\App\Repositories\Model',

License

Laravel Model Repository is free software distributed under the terms of the MIT license.

lerouse/laravel-repository 适用场景与选型建议

lerouse/laravel-repository 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 17.11k 次下载、GitHub Stars 达 5, 最近一次更新时间为 2020 年 10 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「database」 「repository」 「laravel」 「eloquent」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 lerouse/laravel-repository 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 lerouse/laravel-repository 我们能提供哪些服务?
定制开发 / 二次开发

基于 lerouse/laravel-repository 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 5
  • Watchers: 1
  • Forks: 4
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-10-06