Skip to content

rmitesh/laravel-pantry

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

21 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Laravel Pantry

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.

About

To generate a Laravel class equipped with essential default features.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages