定制 phucnguyenvn/laravel-eloquent-repository 二次开发

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

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

phucnguyenvn/laravel-eloquent-repository

Composer 安装命令:

composer require phucnguyenvn/laravel-eloquent-repository

包简介

Repository pattern for Eloquent ORM

README 文档

README

Package to assist the implementation of the Repository Pattern using Eloquent ORM.

Installation

Installing via Composer

composer require phucnguyenvn/laravel-eloquent-repository

To configure the package options, declare the Service Provider in the config/app.php file.

'providers' => [
    ...
    phucnguyenvn\EloquentRepository\Providers\RepositoryServiceProvider::class,
],

If you are using version 5.5 or higher of Laravel, the Service Provider is automatically recognized by Package Discover.

Usage

To get started you need to create your repository class and extend the EloquentRepository available in the package. You also have to set the Model that will be used to perform the queries.

Example:

namespace App\Repositories;

use App\Models\User;
use phucnguyenvn\EloquentRepository\Repositories\EloquentRepository;

class UserRepository extends EloquentRepository
{
    /**
     * Repository constructor.
     */
    public function __construct(User $user){
        parent::__construct($user);
    }
}

Now it is possible to perform queries in the same way as it is used in Eloquent.

namespace App\Repositories;

use App\Models\User;
use phucnguyenvn\EloquentRepository\Repositories\EloquentRepository;

class UserRepository extends EloquentRepository
{
    /**
     * Repository constructor.
     */
    public function __construct(User $user){
        parent::__construct($user);
    }
    
    public function getAllUser(){
        return $this->all();
    }
    
    public function getByName($name) {
        return $this->where("name", $name)->get();
    }
    
    // You can create methods with partial queries
    public function filterByProfile($profile) {
        return $this->where("profile", $profile);
    }
    
    // Them you can use the partial queries into your repositories
    public function getAdmins() {
        return $this->filterByProfile("admin")->get();
    }
    public function getEditors() {
        return $this->filterByProfile("editor")->get();
    }
    
    // You can also use Eager Loading in queries
    public function getWithPosts() {
        return $this->with("posts")->get();
    }
}

To use the class, just inject them into the controllers.

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Repositories\UserRepository;

class UserController extends Controller
{
    protected function index(UserRepository $repository) {
        return $repository->getAdmins();
    }
}

The injection can also be done in the constructor to use the repository in all methods.

namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Repositories\UserRepository;
class UserController extends Controller
{
    private $userRepository;
	public function __construct()(UserRepository $userRepository) {
        $this->userRepository = $userRepository;
    }
    
    public function index() {
        return $this->userRepository->getAllUsers();
    }
    
}

The Eloquent/QueryBuilder methods are encapsulated as protected and are available just into the repository class. Declare your own public data access methods within the repository to access them through the controller.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: BSD-3-Clause
  • 更新时间: 2019-01-21

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固