承接 jetcod/eloquent-repository 相关项目开发

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

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

jetcod/eloquent-repository

最新稳定版本:1.6.0

Composer 安装命令:

composer require jetcod/eloquent-repository

包简介

This package is an implementation of repository pattern in laravel.

README 文档

README

Actions Status

Latest Stable Version Total Downloads License

Requirements

  • PHP ^8.0 or higher
  • Laravel 9.0 or higher
  • Eloquent 8.0 or higher

Installation

You can install the library using Composer:

composer require jetcod/eloquent-repository

Usage

To use the library, you need to create a repository class for each of your Eloquent models. You can either extend the Jetcod\LaravelRepository\Eloquent\BaseRepository class to get started or use artisan command to generate the repository class:

php artisan make:repository UserRepository

Here's an example of a UserRepository class:

<?php

namespace App\Repositories;

use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Jetcod\LaravelRepository\Eloquent\BaseRepository

class UserRepository extends BaseRepository
{
    protected function getModelName()
    {
        return User::class;
    }
}

In this example, the UserRepository extends the BaseRepository class and connects the repository to User mode by implementing getModelName() method . You can then use the repository to perform CRUD operations on the User model, like this:

<?php

namespace App\Services;

use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Jetcod\LaravelRepository\Eloquent\BaseRepository

class UserService
{
    protected $repository;

    protected function __construct(UserRepository $repository)
    {
        $this->repository = $repository;
    }

    public function getAdmins()
    {
        return $this->repository->findBy([
            ['role', '=', 'ADMIN']
        ]);
    }
}

API

The following methods are available in the BaseRepository class:

  • find(int $id): Find a model by ID.
  • findAll(): Get all models.
  • findBy(array $conditions, array $relations = [], bool $paginate = true, int $pageSize = 10): Find all models and their identified relations by conditions. The result can ne either paginated or return as a collection.
  • findOneBy(array $conditions, array $relations = []): Find a model and its identified relations by conditions.
  • with($relations, $callback = null): Set the relationships that should be eager loaded.
  • countBy(array $conditions): Count models by conditions.
  • delete($model): Delete a model
  • insert(array $rows): Bulk insert data
  • create(array $attributes): Create a new model.
  • updateOrCreate(array $data, array $conditions): Create or update a record matching the attributes, and fill it with values
  • update(Model $model, array $data, array $fillable = []): Update an existing model.
  • fill(Model $model, array $data, array $fillable = []): Fill the given model by the given array of data.
  • delete($model): Delete a model.
  • destroy($ids): Destroy an array or collection of ids.
  • query(): Query builder instance.

You can also extend the BaseRepository * class to add custom methods for your specific use case.

Contributing

If you find any bugs or have ideas for new features, please open an issue or submit a pull request on GitHub.

License

My Eloquent Repository Library is open-sourced software licensed under the MIT license.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-02-17

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固