creode / laravel-repository
暴露了一个基础仓库类,允许与自定义模型进行交互。
1.1.0
2024-06-23 09:10 UTC
Requires
- illuminate/database: ^10.0 | ^11.0
This package is auto-updated.
Last update: 2024-09-23 09:51:56 UTC
README
暴露了一个基础仓库类,允许与自定义模型进行交互。
安装
composer require creode/laravel-repository
用法
要使用此包,您必须扩展基础仓库抽象类,并重写getModel()方法以返回您希望使用的模型类。
use Creode\LaravelRepository\BaseRepository; class MyRepository extends BaseRepository { /** * Returns the model class to use for this repository. * * @return string */ protected function getModel() { return MyModel::class; } }
然后可以在任何其他类中调用此仓库。
在您的代码中利用此类的优点意味着您可以使用命名空间配置字符串在应用程序中重新绑定模型类。通过将任何硬编码的模型类引用直接与仓库交换,您仍然可以在它上调用标准模型函数,但它允许在运行时进行动态绑定。
有关仓库模式的更多详细信息,请参阅这篇文章:[仓库模式的好处](https://www.twilio.com/blog/repository-pattern-in-laravel-application)。