lab2view/laravel-repository-generator

从 Eloquent 模型生成仓库

1.9.1 2022-08-16 11:26 UTC

This package is auto-updated.

Last update: 2024-09-25 20:27:42 UTC


README

Latest Stable Version Total Downloads Monthly Downloads License

Laravel Repositories Generator 是一个用于从 Eloquent 模型生成仓库的 Laravel 包。

安装

从您的终端运行以下命令

composer require "lab2view/laravel-repository-generator"

用法

从 Models 文件夹中的 Eloquent 模型生成仓库类

php artisan make:repositories

在控制器中使用生成的仓库

<?php namespace App\Http\Controllers;

use App\Repositories\PostRepository;

class PostController extends Controller {

    private $postRepository;
    
    public function __construct(PostRepository $postRepository)
    {
        $this->postRepository = $postRepository;
    }

    public function index() {
        return response()->json($this->postRepository->getAll());
    }
}

可用方法

以下方法可用

Lab2view\RepositoryGenerator\RepositoryInterface
    public function exists(string $key, $value, $withTrashed = false)

    public function getByAttribute(string $attr_name, $attr_value, $relations = [], $withTrashed = false, $selects = [])

    public function getPaginate(int $n, $relations = [], $withTrashed = false, $selects = []);

    public function store(Array $inputs)

    public function getById($id, $relations = [], $withTrashed = false, $selects = [])

    public function search($key, $value, $relations = [], $withTrashed = false, $selects = [])

    public function getAll($relations = [], $withTrashed = false, $selects = [])

    public function countAll($withTrashed = false)

    public function getAllSelectable($key)

    public function update($id, Array $inputs)

    public function destroy($id)

    public function destroyAll()

    public function forceDelete($id)

    public function restore($id)

示例用法

在仓库中创建新帖子

$post = $this->postRepository->store($request->all());

更新现有帖子

$post = $this->postRepository->update($post_id, $request->all());

删除帖子

$post = $this->postRepository->destroy($id);

通过 post_id 获取帖子

$post = $this->postRepository->getById($id);

您还可以选择要预加载的关系

$post = $this->postRepository->getById($id, ['comments']);

贡献

感谢您考虑为这个 Laravel 包做出贡献。

鸣谢

此包受到 优秀包的启发:@bosnadev。