PHP code example of rmitesh / laravel-pantry

1. Go to this page and download the library: Download rmitesh/laravel-pantry library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

rmitesh / laravel-pantry example snippets


use App\Pantries\FoodPantry;

class FoodController extends Controller
{
	public function __construct(
	    protected FoodPantry $foodPantry
	) {}
}

use App\Pantries\FoodPantry;

class FoodController extends Controller
{
	/**
	 * @var FoodPantry
	 */
	protected $foodPantry;

	public function __construct(FoodPantry $foodPantry) {
		$this->foodPantry = $foodPantry;
	}
}

$food = $this->foodPantry->get($record);

$foods = $this->foodPantry->getAll([
	// your column name, default '*',
	// conditions,
	// relationships
]);


$foods = $this->foodPantry->getAll(
	conditions: [
		['where', 'status', true ],
		['whereHas', 'relationshipName' ],
	]
);

$foods = $this->foodPantry->getAll([
	'id', 'name', 'created_at'
], [
	'with' => 'relationshipName',
]);

use Rmitesh\LaravelPantry\Pantries\Concerns\HasPagination;

class FoodPantry extends Pantry
{
	use HasPagination; // add this in your Pantry class
}


protected static ?int $perPageRecordLenght = 20;

$foods = $this->foodPantry->paginate([
		// your column name, default '*'
]);

$foods = $this->foodPantry->paginate([
		// your column name, default '*'
], [
    'with' => 'ingredients',
]);

$food = $this->foodPantry->store($request->validated());

$food = $this->foodPantry->update($key, $request->validated());

$food = $this->foodPantry->destroy($key);

$food = $this->foodPantry->destroyAll($ids);

$foods = $this->foodPantry->getAll(
    relationships: [
        'with' => 'ingredients',
    ]
);

use Illuminate\Database\Eloquent\Builder;

$foods = $this->foodPantry->getAll(
    relationships: [
        'with' => [
        	'ingredients',
        	function ( Builder $query ) {
	        	// your conditions
	        }
        ],
    ]
);

use Illuminate\Database\Eloquent\Builder;

$foods = $this->foodPantry->getAll(
    relationships: [
        'with' => [
        	[
        		'ingredients' => function ( Builder $query ) {
		        	// your conditions
		        },
        	],
        	[
        		// second relationship ...
        	],
        ],
    ]
);
bash
php artisan make:pantry Food
bash
php artisan make:pantry Food --model=FoodItem

|── app
    └── Http
        └── Controllers
            └── FoodController.php
    └── Models
        └── Food.php
    └── Pantries
        └── FoodPantry.php