rmitesh/laravel-pantry
Composer 安装命令:
composer require rmitesh/laravel-pantry
包简介
To generate a Laravel class equipped with essential default features.
README 文档
README
You may know and worked with repository pattern in the Laravel.
Now, here I am going to introduce Laravel Pantry where you get all things in one place same as Food Pantry Shop.
Installation
You can install the package via composer:
composer require rmitesh/laravel-pantry
Create a new Pantry
To create a new Pantry class
php artisan make:pantry Food
Based on the Pantry class name, automatically consider model name will be same as Pantry class.
Or you want to generate for specific model.
php artisan make:pantry Food --model=FoodItem
It will create FoodPantry class inside the app/Pantries directory.
and Food is model class name.
So, final directory structure will look like this.
|── app
└── Http
└── Controllers
└── FoodController.php
└── Models
└── Food.php
└── Pantries
└── FoodPantry.php
How to use
In your FoodController add __construct function.
use App\Pantries\FoodPantry; class FoodController extends Controller { public function __construct( protected FoodPantry $foodPantry ) {} }
OR
use App\Pantries\FoodPantry; class FoodController extends Controller { /** * @var FoodPantry */ protected $foodPantry; public function __construct(FoodPantry $foodPantry) { $this->foodPantry = $foodPantry; } }
Available Methods
get()
To fetch single record.
$food = $this->foodPantry->get($record);
| Argument | Value |
|---|---|
| $record | It accepts model ID ( Illuminate\Database\Eloquent\Model, string, id ) |
| $columns | Column names in array format, by default it will be ['*'] |
| $relationships | It's Optional, You can pass relationship arguments in array. see relationships examples |
It will also works with Route Model Binding.
It will return eloquent class on record found, else it will throw 404 | Not found.
getAll()
To fetch multiple records.
$foods = $this->foodPantry->getAll([ // your column name, default '*', // conditions, // relationships ]);
With conditions, it's an optional parameter.
$foods = $this->foodPantry->getAll( conditions: [ ['where', 'status', true ], ['whereHas', 'relationshipName' ], ] );
With relationships, it's an optional parameter. More details, check Fetch data with relationships.
$foods = $this->foodPantry->getAll([ 'id', 'name', 'created_at' ], [ 'with' => 'relationshipName', ]);
| Argument | Value |
|---|---|
| $columns | Column names in array format, by default it will be ['*'] |
| $conditions | It's Optional, You can add conditions, by default it will be empty array. |
| $relationships | It's Optional, You can pass relationship arguments in array. see relationships examples |
As a return it will give collection.
paginate()
To fetch records with pagination.
To use paginate() method, you have to use Rmitesh\LaravelPantry\Pantries\Concerns\HasPagination.
use Rmitesh\LaravelPantry\Pantries\Concerns\HasPagination; class FoodPantry extends Pantry { use HasPagination; // add this in your Pantry class }
For change per page records limit, you can override the $perPageRecordLenght in your Pantry class.
protected static ?int $perPageRecordLenght = 20;
By default
$perPageRecordLenghtvalue set is20and that is enough for per page records.
$foods = $this->foodPantry->paginate([ // your column name, default '*' ]);
Or with relationships
$foods = $this->foodPantry->paginate([ // your column name, default '*' ], [ 'with' => 'ingredients', ]);
As a return it will give Illuminate\Pagination\LengthAwarePaginator collection.
store()
To store data in table.
You need to just pass the array data.
$food = $this->foodPantry->store($request->validated());
| Argument | Value |
|---|---|
| $data | Array key-value pair |
As a return it will give created model instance.
update()
To update data in table.
$food = $this->foodPantry->update($key, $request->validated());
| Argument | Value |
|---|---|
| $key | It accepts record ID ( Illuminate\Database\Eloquent\Model, string, id ) |
| $data | Array key-value pair |
As a return it will give updated model instance.
destroy()
To delete single record.
$food = $this->foodPantry->destroy($key);
| Argument | Value |
|---|---|
| $key | It accepts record ID ( Illuminate\Database\Eloquent\Model, string, id ) |
As a return true on record deleted, false on failure.
destroyAll()
To delete multiple records at once.
$food = $this->foodPantry->destroyAll($ids);
| Argument | Value |
|---|---|
| $ids | It accepts record ID in array or Illuminate\Support\Collection format |
As a return true on record deleted, false on failure.
Fetch data with relationships.
To add relationships, you can pass in array format.
Let's take an example,
One Food has many ingredients. So, to fetch Food records along with their Ingredients.
$foods = $this->foodPantry->getAll( relationships: [ 'with' => 'ingredients', ] );
For relationships, as key it should be relationship name like
with,withWhereHas,whereHas,withCount,whereBelongsToand so on.
Moreover, if you want to add Closure function then
use Illuminate\Database\Eloquent\Builder; $foods = $this->foodPantry->getAll( relationships: [ 'with' => [ 'ingredients', function ( Builder $query ) { // your conditions } ], ] );
Or if you have multiple relationships then,
use Illuminate\Database\Eloquent\Builder; $foods = $this->foodPantry->getAll( relationships: [ 'with' => [ [ 'ingredients' => function ( Builder $query ) { // your conditions }, ], [ // second relationship ... ], ], ] );
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Buy me a Coffee
Credits
License
The MIT License (MIT). Please see License File for more information.
rmitesh/laravel-pantry 适用场景与选型建议
rmitesh/laravel-pantry 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 53 次下载、GitHub Stars 达 12, 最近一次更新时间为 2023 年 09 月 17 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「repository」 「laravel」 「rmitesh」 「laravel-pantry」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 rmitesh/laravel-pantry 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 rmitesh/laravel-pantry 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 rmitesh/laravel-pantry 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
repository php library
Creates a Filament table action.
Laravel 5 - Repositories to the database layer
Structure for the Laravel Service-Repository Pattern
Rinvex Repository is a simple, intuitive, and smart implementation of Active Repository with extremely flexible & granular caching system for Laravel, used to abstract the data layer, making applications more flexible to maintain.
Base Skeleton for Laravel Application
统计信息
- 总下载量: 53
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 12
- 点击次数: 17
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-09-17