定制 imjonos/laravel-base-repository 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

imjonos/laravel-base-repository

最新稳定版本:1.1.1

Composer 安装命令:

composer require imjonos/laravel-base-repository

包简介

Laravel base repository

README 文档

README

Latest Version on Packagist
Total Downloads

A generic base repository class for Laravel projects that provides a clean and consistent way to interact with Eloquent models. It simplifies CRUD operations and makes your code more maintainable, testable, and scalable.

🧩 Overview

This package provides an abstract EloquentRepository class that implements the EloquentRepositoryInterface. It wraps common model interactions into reusable methods, making it easier to manage data access logic in your Laravel applications.

🛠 Installation

Install the package via Composer:

composer require imjonos/laravel-base-repository

✅ Usage

1. Create Your Repository Class

Create a new repository class that extends EloquentRepository and specifies the model class:

namespace App\Repositories;

use App\Models\Order;
use Nos\BaseRepository\EloquentRepository;

class OrderRepository extends EloquentRepository
{
    protected string $class = Order::class;
}

2. Use the Repository in a Controller or Service

Inject the repository and use its methods:

namespace App\Http\Controllers;

use App\Repositories\OrderRepository;
use Illuminate\Http\Request;

class OrderController extends Controller
{
    protected $repository;

    public function __construct(OrderRepository $repository)
    {
        $this->repository = $repository;
    }

    public function index()
    {
        $orders = $this->repository->all();
        return view('orders.index', compact('orders'));
    }

    public function store(Request $request)
    {
        $order = $this->repository->create($request->all());
        return redirect()->route('orders.show', $order->id);
    }
}

🔧 Available Methods

Method Description
all() Get all records
count() Count all records
create(array $data) Create a new record
update(int $id, array $data) Update a record by ID
exists(int $id) Check if a record exists
find(int $id) Find a record by ID (throws exception if not found)
delete(int $id) Delete a record by ID
query() Return a query builder instance for custom queries

🌐 Project Structure

vendor/
└── imjonos/
    └── laravel-base-repository/
        ├── src/
        │   └── EloquentRepository.php
        └── interfaces/
            └── EloquentRepositoryInterface.php

📦 Requirements

  • PHP 8.0+
  • Laravel 9+

🧪 Testing

You can easily mock the repository interface in your tests, which helps keep your application logic decoupled from the database and improves test coverage.

🚀 Contributing

Please see contributing.md for details and a todolist.

📝 License

This package is open-sourced software licensed under the MIT license. Please see the license file for more information.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2022-05-27