承接 wimski/laravel-model-repositories 相关项目开发

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

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

wimski/laravel-model-repositories

最新稳定版本:v6.0.0

Composer 安装命令:

composer require wimski/laravel-model-repositories

包简介

Laravel model repositories

README 文档

README

Latest Stable Version Coverage Status PHPUnit PHPStan

Laravel Model Repositories

Generic repository pattern for Laravel Eloquent models.

Changelog

View the changelog.

Usage

Install package

composer require wimski/laravel-model-repositories

Setup service provider

This package is NOT loaded using package discovery. You should create your own service provider that extends the one from this package and add it to your config manually.

app/Providers/RepositoryServiceProvider.php

<?php namespace App\Providers; use Wimski\ModelRepositories\Providers\ModelRepositoryServiceProvider; class RepositoryServiceProvider extends ModelRepositoryServiceProvider { protected array $repositories = [ // add your repository bindings here ]; }

config/app.php

<?php return [ 'providers' => [ /*  * Application Service Providers...  */ App\Providers\RepositoryServiceProvider::class, ], ];

Generate a repository

php artisan make:repository App\\Models\\MyModel

This will create the following files:

  • MyModelRepositoryInterface.php
  • MyModelRepository.php

The namespace and file location depend on the namespace configuration.

Alternatively you can completely override the contract and repository FQN by using the command options. The namespace matching and class name suffixing will be skipped.

php artisan make:repository App\\Models\\MyModel --contract=Foo\\Bar --repository=Lorem\\Ipsum

Add repository binding

Set up the binding of your new repository in the service provider.

protected array $repositories = [ MyModelRespositoryInterface::class => MyModelRespository::class, ];

Example usage

class SomeService { protected MyModelRespositoryInterface $repository; public function __construct(MyModelRespositoryInterface $repository) { $this->repository = $repository; } public function doSomething($id): void { $myModel = $this->repository->findOrFail($id); } }

Publishing configuration file

Because this package does not register its own service provider, your own service provider should be referenced when publishing this package's configuration file.

php artisan vendor:publish --provider=App\Providers\RepositoryServiceProvider

Namespace configuration

The namespaces configuration is used to determine what the namespaces of your repository classes - and locations of the subsequent files - should be. Each configuration exists of three parts:

  • models
  • contracts
  • repositories

When generating a repository for a model the command will look for a namespace configuration which has a models part that matches the supplied model. The contracts and repositories parts of that configuration are then used as the namespaces for the repository classes.

Assumptions

  • The namespaces follow the PSR-4 file location convention.
  • The namespaces are within the Laravel app directory.

Default configuration

'namespaces' => [ [ 'models' => 'App\\Models', 'contracts' => 'App\\Contracts\\Repositories', 'repositories' => 'App\\Repositories', ], ],

When generating a repository for App\Models\MyModel, the following two classes will be created:

  • App\Contracts\Repositories\MyModelRepositoryInterface
  • App\Repositories\MyModelRepository

Multiple configurations

You can have multiple namespace configurations, for example when using domains.

'namespaces' => [ [ 'models' => 'App\\DomainA\\Models', 'contracts' => 'App\\DomainA\\Contracts\\Repositories', 'repositories' => 'App\\DomainA\\Repositories', ], [ 'models' => 'App\\DomainB\\Models', 'contracts' => 'App\\DomainB\\Contracts\\Repositories', 'repositories' => 'App\\DomainB\\Repositories', ], ],

Specificity

The first match will be used so if you have overlapping namespace configurations, make sure to have the more specific ones on top.

'namespaces' => [ [ 'models' => 'App\\Models\\SpecificModels', ... ], [ 'models' => 'App\\Models', ... ], ],

Available methods

See interface.

Stub customization

See Laravel's documentation about stub customization. This package adds the following stub files:

  • model.repository.interface.stub
  • model.repository.stub

PHPUnit

composer test

PHPStan

composer analyze

Credits

License

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

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-01-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固