承接 rmitesh/laravel-pantry 相关项目开发

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

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

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.

laravel-pantry

Latest Stable Version Total Downloads Laravel v10.x PHP 8.1

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 $perPageRecordLenght value set is 20 and 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, whereBelongsTo and 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

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 我们能提供哪些服务?
定制开发 / 二次开发

基于 rmitesh/laravel-pantry 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

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

GitHub 信息

  • Stars: 12
  • Watchers: 2
  • Forks: 1
  • 开发语言: PHP

其他信息

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