nikidze/laravel-repository-generator

从Eloquent模型生成仓库

v0.0.1 2022-03-08 11:41 UTC

This package is auto-updated.

Last update: 2024-09-08 17:18:06 UTC


README

Latest Stable Version Total Downloads Monthly Downloads License

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

安装

从您的终端运行以下命令

composer require "nikidze/laravel-repository-generator"

用法

首先,从模型文件夹中的Eloquent模型生成您的仓库类。

php artisan make:repositories

最后,在控制器中使用该仓库

<?php namespace App\Http\Controllers;

use App\Repositories\PostRepository;

class PostController extends Controller {

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

    public function index() {
        return response()->json($this->post->all());
    }
}

可用方法

以下方法可用

Nikidze\RepositoryGenerator\RepositoryInterface
    public function all();

    public function trashOnly();

    public function find($id);

    public function findTrash($id);

    public function findBy($column, $value);

    public function recent($limit);

    public function store($data);

    public function update($data, $id);

    public function trash($id);

    public function restore($id);

    public function destroy($id);

示例用法

在仓库中创建新文章

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

更新现有文章

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

删除文章

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

通过post_id获取文章

$post = $this->post->find($id);

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

$post = $this->post->find($id);

致谢

此包受到了此优秀包的启发,由@tcharod提供。