PHP code example of mratiebatie / laravel-repositories
1. Go to this page and download the library: Download mratiebatie/laravel-repositories 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/ */
mratiebatie / laravel-repositories example snippets
/**
* In your routes/web.php
*/
Route::get('/', function (\App\Repositories\ProductRepository $productRepo) {
// Use any Eloquent feature directly
$productRepo->all()->dd();
// Use your custom repository methods
echo $productRepo->findBySku(12345)->name;
// You can even query relations
echo $productRepo->first()->category;
});
/**
* In your routes/web.php
*/
use App\Facades\ProductRepository;
Route::get('/', function () {
// Use any Eloquent feature directly
ProductRepository::all()->dd();
// Use your custom repository methods
echo ProductRepository::findBySku(12345)->name;
// You can even query relations
echo ProductRepository::first()->category;
});
php
namespace App\Repositories;
use MrAtiebatie\Repository;
use App\Models\Product;
class ProductRepository
{
use Repository;
/**
* The model being queried.
*
* @var Model
*/
protected $model;
/**
* Constructor
*/
public function __construct()
{
// setup the model
$this->model = app(Product::class);
}
}
php
namespace App\Repositories;
use MrAtiebatie\Repository;
use App\Models\Product;
class ProductRepository
{
use Repository;
/**
* The model being queried.
*
* @var Model
*/
protected $model;
/**
* Constructor
*/
public function __construct()
{
// setup the model
$this->model = app(Product::class);
}
/**
* Find published products by SKU
*
* @param {int} $sku
*
* @return {Product}
*/
public function findBySku(int $sku): Product
{
// using 'whereIsPublished' and 'whereSku' scopes
// defined on the Product model
return $this->whereIsPublished(1)
->whereSku($sku)
->first();
}
}
php
namespace App\Facades;
use Illuminate\Support\Facades\Facade;
class ProductRepository extends Facade
{
protected static function getFacadeAccessor()
{
return 'ProductRepository';
}
}
php
namespace App\Providers;
use App\Repositories\ProductRepository;
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register()
{
$this->app->bind('ProductRepository', function () {
return new ProductRepository();
});
}
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
//
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.