承接 longaodai/repository-pattern 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

longaodai/repository-pattern

最新稳定版本:v1.0.2

Composer 安装命令:

composer require longaodai/repository-pattern

包简介

This is library support for project Laravel use repository design pattern

README 文档

README

This package provides a base repository implementation following the repository design pattern in PHP.

Installation

You can install this package via Composer. Run the following command in your terminal:

composer require longaodai/repository-pattern

In config/app.php add provider

'providers' => ServiceProvider::defaultProviders()->merge([
        /*
         * Package Service Providers...
         */

        /*
         * Application Service Providers...
         */
        \LongAoDai\Repositories\RepositoryServiceProvider::class,
    ])->toArray(),

Public config file pattern.php

php artisan vendor:publish --tag=longaodai-repository-pattern

Result exec 🍀:

 Copying file [vendor/longaodai/repository-pattern/config/pattern.php] to [config/pattern.php] ............................................... DONE

😀 Now, you can the create repository by command instead of creating it manually .

php artisan setup:repository Comment

Result exec 🍀:

Repository Comment created successfully !!!
Implement: App\Repositories\CommentEloquentRepository.php
Interface: App\Repositories\CommentRepositoryInterface.php

After running the first repository creation command, in the app/Providers folder there will be auto create a RepositoryServiceProvider.php file. Pls add more into config/app.php for register custom provider.

'providers' => ServiceProvider::defaultProviders()->merge([
        /*
         * Package Service Providers...
         */

        /*
         * Application Service Providers...
         */
        \LongAoDai\Repositories\RepositoryServiceProvider::class,
        \App\Providers\RepositoryServiceProvider::class,
    ])->toArray(),

Usage

To use the base repository in your project, you need to create repository classes that extend the BaseRepository class provided by this package.

Here's an example of how to create a repository class:

use LongAoDai\Repositories\BaseRepository;

class UserRepository extends BaseRepository
{
    /**
     * Specify the model class name.
     *
     * @return mixed
     */
    public function model()
    {
        return User::class;
    }
}

public function getList($data = null, $options = null): mixed
{
    $this->method('select', [
        'name', 'email'
    ]);

    return parent::getList($data, $options);
}

protected function filter($params): UserRepository
{
    if ($params->get('name')) {
        $this->method('where', 'name', 'like', '%' . $params->get('name') . '%');
    }

    if ($params->get('id')) {
        $this->method('where', 'id', $params->get('id'));
    }

    return parent::filter($params);
}

protected function mark($params): UserRepository
{
    if ($params->option('id')) {
        $this->method('where', 'id', $params->option('id'));
    }

    return parent::mark($params);
}

You can then use methods provided by the BaseRepository class such as all(), find(), create(), update(), and delete() in your repository classes.

In controller file you can use

public function index()
{
    $repository = app(UserRepository::class);
    // Get list with pagination
    $user = $repository->getList($data = null, $options = null);
    // Get all data
    $user = $repository->all($data = null, $options = null);
    // Find by id
    $user = $repository->find(['id' => 1]);
    // Update data
    $user = $repository->update(
        collect([
            'name' => 'Vo Chi Long'
        ]),
        collect(['id' => 1])
    );
    // Create data
    $user = $repository->create(collect([
        'name' => 'Vo Chi Long',
        'email' => 'vochilong.work@gmail.com',
        'password' => bcrypt('password'),
    ]));
    // Get first by params condition
    $user = $repository->first(collect([
        'name' => 'Long'
    ]));
    // Update or Create by options condition
    $user = $repository->updateOrCreate(collect([
        'name' => 'Vo Chi Long'
    ]), collect([
        'email' => 'vochilong.work@gmail.com'
    ]));
    // Delete data by condition
    $user = $repository->destroy(['id' => 10]);
    
    return json_encode($user ?? []);
}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-03-17