承接 visiarch/laravel-repository 相关项目开发

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

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

visiarch/laravel-repository

最新稳定版本:1.0.0

Composer 安装命令:

composer require visiarch/laravel-repository

包简介

A simple Laravel package to create repository, using artisan commands

README 文档

README

laravel Repository

laravel-repository

Latest Stable Version License

"Buy Me A Coffee"

A Simple Package to create repositories, using artisan commands in laravel.

This package extends the make: commands to help you easily create repository classes in Laravel 9+.

What is Repository ?

Repository is a design pattern used to manage data access logic. It provides abstraction between the application and data access layers, such as databases.

Install

composer require visiarch/laravel-repository

Once it is installed, you can use any of the commands in your terminal.

Usage

Repositories are used to separate data access logic from business logic, allowing developers to change how data is stored and retrieved without affecting business logic.

With interface

php artisan make:repository {name} --i

Without interface

php artisan make:repository {name}

Examples

Create a repository class with interface

php artisan make:repository PostRepository --i

app/Repositories/Interfaces/PostRepositoryInterface.php

<?php

namespace App\Repositories\Interfaces;

/**
 * Interface PostRepositoryInterface
 *
 * This interface defines the methods for the PostRepository class.
 */

interface PostRepositoryInterface
{
   //
}

app/Repositories/PostRepository.php

<?php

namespace App\Repositories;

use App\Repositories\Interfaces\PostRepositoryInterface;

/**
 * Class PostRepository
 *
 * @package App\Repositories
 */

class PostRepository implements PostRepositoryInterface
{
   //
}

app\Providers\AppServiceProvider

public function register(): void
{
    //
    $this->app->bind(
    \App\Repositories\Interfaces\PostRepositoryInterface::class,
    \App\Repositories\PostRepository::class
    );
}

Create a repository class without interface

php artisan make:repository PostRepository

app/Repositories/PostRepository.php

<?php

namespace App\Repositories;

/**
 * Class PostRepository
 *
 * @package App\Repositories
 */

class PostRepository
{
   //
}

Implementation

With Interface

<?php

namespace App\Repositories\Interfaces;

use App\Models\Post;

class PostRepository implements PostRepositoryInterface
{
    public function store(array $data): Post;
}
<?php

namespace App\Repositories;

use App\Models\Post;

class PostRepository implements PostRepositoryInterface
{
    protected $post;

    public function __construct(Post $post){
        $this->post = $post;
    }

    public function store(array $data): Post
    {
        return $this->post->create($data);
    }
}

Without Interface

<?php

namespace App\Repositories;

use App\Models\Post;

class PostRepository
{
    protected $post;

    public function __construct(Post $post){
        $this->post = $post;
    }

    public function store(array $data): Post
    {
        return $this->post->create($data);
    }
}

How to implement it on the controller?

<?php

namespace App\Repositories;

use App\Models\Post;
use App\Repositories\Interfaces\PostServiceInterface;
use App\Http\Requests\PostRequest;

class PostController extends Controller
{
    protected $postService;

    public function __construct(PostServiceInterface $postService)
    {
        $this->postService = $postService;
    }

    public function store(PostRequest $request): RedirectResponse
    {
        $data = $request->validated();
        $this->postService->store($data);

        return redirect()->route('posts.index')->with('success', __('post created'));
    }
}
Just change PostRepositoryInterface to PostRepository if you are implementing without interface

Contributing

Please feel free to fork this package and contribute by submitting a pull request to enhance the functionalities.

How can I thank you?

Why not star the github repo? I'd love the attention! Why not share the link for this repository on any social media? Spread the word!

Thanks! visiarch

License

The MIT License (MIT). Please see License File for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-06-29