littlebug/laravel-repository

laravel 的仓库模式!

v2.0.7 2021-09-22 05:00 UTC

This package is auto-updated.

Last update: 2024-09-29 05:28:24 UTC


README

Progress Latest Stable Version Total Downloads Latest Unstable Version License GitHub stars GitHub issues GitHub forks Laravel

切换中文 | 仓库使用说明

简介

laravel-repositorylaravel 模型 提供基本的 repository 类。该包旨在提供更多外部方法和更友好的编辑器提示;分层代码,repository 负责外部业务逻辑处理,model 仅负责定义字段、属性、查询条件和数据表的返回值。它不参与具体的逻辑操作,也不服务于控制层。

与直接使用 model 相比的优势

  • 解决 model 在添加或修改时无法自动处理额外字段的问题
  • 优化 model 查询的链式调用,可以直接使用数组查询
  • 通过查询条件和查询字段自动处理相应的关联数据查询
  • 提供更友好的编辑器提示

安装

安装要求

  • PHP >= 7.0.0
  • Laravel >= 5.5.0

1.1 安装包

composer require littlebug/laravel-repository:2.0.*

或在您的 composer.json 文件的 require 部分添加以下内容

"littlebug/laravel-repository": "2.0.*"

然后运行 composer update

1.2 使用命令生成 modelrepository

假设您的数据库中有用户,或者您将用户替换为数据库中的表名。

php artisan core:model --table=users --name=User

命令将在以下位置执行

  • app/Models/ 文件夹下生成 User 文件
  • app/Repositories/ 文件夹下生成 UserRepository 文件

1.3 在控制器中使用 repository

<?php

use Illuminate\Routing\Controller;
use Littlebug\Repository\Tests\Stubs\UserRepository;

class UsersController extends Controller 
{
    /**
     * @var UserRepository
     */
    private $userRepository;
    
    public function __construct(UserRepository $userRepository)
    {
        $this->userRepository = $userRepository;
    }
    
    public function index()
    {
        // Paging queries, returning paging objects
        $paginate = $this->userRepository->paginate([
            'name:like' => 'test', 
            'status'    => [1, 2], // Automatically converts to an in query,

            // Add complex queries and generate SQL: ("users"."status" = ? or "users"."age" >= ? or ("users"."status" = ? and "users"."age" != ?))
            // More instructions: https://wanchaochao.github.io/laravel-repository/?page=repository#5.3-%E9%A2%84%E5%AE%9A%E4%B9%89%E5%AD%97%E6%AE%B5%E6%9F%A5%E8%AF%A2
            'or' => [
                'status'  => 1,
                'age:gte' => 26,
                'and'     => [
                    'status'  => 1,
                    'age:neq' => 24,
                ],
            ],
        ], [
            'user_id',
            'username',
            
            // Statistical associated data; withCount  
            'posts_count',      

            // Query the field information for the association table if the model defines the association relationship
            'ext' => [
                'user_id',
                'ext_avatar', 
            ],
        ]);
        
        return view('users.index', compact('paginate'));
    }
    
    public function create()
    {
        // Add data and return an array
        $user = $this->userRepository->create(request()->all());
        dump($user);
    }
    
    public function update()
    {
        // Modify the data and return the number of modified rows
        $row = $this->userRepository->update(request()->input('id'), request()->all());
        dump($row);
    }
    
    public function delete()
    {
        // Deletes data and returns the number of rows deleted
        $row = $this->userRepository->delete(request()->input('id'));
        dump($row);
    }
}

除了上述的注入方法调用外,您还可以使用静态方法调用;如下所示

use Littlebug\Repository\Tests\Stubs\UserRepository;

$paginate = UserRepository::instance()->paginate(['status' => 1]);

// Query a piece of data and return an array
$user = UserRepository::instance()->find(['status' => 1, 'id:gt' => 2]);

1.4 其他常见方法

检索数据

统计查询

创建或修改数据

1.5 更多文档

请查看更多关于 repository 的信息

更多代码生成命令

命令支持指定数据库连接,例如 --table=dev.users

  1. core:model 通过查询数据库表信息生成 model 类文件和 repository 类文件

    php artisan core:model --table=users --name=User
  2. core:repository 生成 repository 类文件

    php artisan core:repository --model=User --name=UserRepository
  3. core:request 通过查询数据库表信息生成 request 验证类文件

    php artisan core:request --table=users --path=Users

命令参数详情

commands of generate code

感谢 jinxing.liu 和七 💐🌹

如果我的仓库对您有帮助,请给我一个 star 鼓励我~ ✨,我会继续维护这个项目。