hora/laravel-common-command

Laravel 常用命令

dev-master 2020-08-26 08:38 UTC

This package is auto-updated.

Last update: 2024-09-26 18:10:40 UTC


README

A Laravel 包帮助您生成一些常用命令,如仓库...

安装

composer require hora/laravel-common-command
php artisan vendor:publish --provider="Hora\LaravelCommonCommand\LaravelCommonCommandServiceProvider"

用法

尝试使用以下命令生成仓库和接口

php artisan make:repository Product

生成的文件看起来像这样

namespace App\Repositories\Eloquents;

use App\Interfaces\ProductRepositoryInterface;
use App\Models\Product;

class ProductRepository implements ProductRepositoryInterface
{

    protected $product;

    /**
     * Product constructor.
     * @param Product $product
     */
    public function __construct(Product $product)
    {
        $this->product = $product;
    }

}
namespace App\Interfaces;

interface ProductRepositoryInterface
{

}

配置

默认情况下,它将在 Repositories 目录中生成仓库文件,并在 Interfaces 目录中生成接口。

您可以在文件 config/laravel-common-command 中修改它

选项

如果您想创建类似 EloquentRepository 的内容,可以在命令中添加前缀选项

例如这样

php artisan make:repository Product --prefix=Eloquent

您也可以使用模板创建仓库

php artisan make:repository Product --template
namespace App\Repositories;

use App\Interfaces\ProductRepositoryInterface;
use App\Models\Product;

class ProductRepository implements ProductRepositoryInterface
{

    protected $product;

    /**
     * Product constructor.
     * @param Product $product
     */
    public function __construct(Product $product)
    {
        $this->product = $product;
    }

    /**
     * @param $id
     * @return mixed
     */
    public function find($id)
    {
        return $this->product->find($id);
    }

    /**
     * @param $id
     * @return mixed
     */
    public function findOrFail($id)
    {
        return $this->product->findOrFail($id);
    }

    /**
     * @param array $data
     * @return mixed
     */
    public function create(array $data)
    {
        return $this->product->create($data);
    }

    /**
     * @param array $data
     * @param $id
     * @return mixed
     */
    public function update(array $data, $id)
    {
        $product = $this->findOrFail($id);
        return $product->update($data);
    }

    /**
     * @param $id
     * @return mixed
     */
    public function delete($id)
    {
        $product = $this->findOrFail($id);
        return $product->delete();
    }

}
interface ProductRepositoryInterface
{
    /**
     * @param $id
     * @return mixed
     */
    public function find($id);

    /**
     * @param $id
     * @return mixed
     */
    public function findOrFail($id);

    /**
     * @param array $data
     * @return mixed
     */
    public function create(array $data);

    /**
     * @param array $data
     * @param $id
     * @return mixed
     */
    public function update(array $data, $id);

    /**
     * @param $id
     * @return mixed
     */
    public function delete($id);
}