1. Go to this page and download the library: Download guilhermegonzaga/repository 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/ */
guilhermegonzaga / repository example snippets
public function first($columns = ['*'], $fail = true);
public function find($id, $columns = ['*'], $fail = true);
public function findBy($attribute, $value);
public function where(array $where, $boolean = 'and');
public function create(array $data, $force = true);
public function update(array $data, $id = null, $force = true);
public function delete($id = null);
public function paginate($limit = 15, $columns = ['*'], $pageName = 'page');
public function exists();
public function random($qtd = 15);
public function scopes(Closure $scope, $boolean = 'and');
public function criteria($class, array $args = []);
public function with($relations);
public function withBoot();
public function withoutBoot();
public function all($columns = ['*']);
public function get($columns = ['*']);
namespace App\Repositories;
use GuilhermeGonzaga\Repository\Eloquent\Repository;
class CategoryRepository extends Repository
{
public function model()
{
return \App\Category::class;
}
// Optional method, global rules
public function boot()
{
$this->findBy('active', true);
$this->orderBy('created_at', 'desc');
}
// Other methods
}
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Category extends Model
{
protected $table = 'categories';
protected $fillable = [
'name',
'parent_id',
...
];
...
}
use App\Repositories\CategoryRepository;
class CategoriesController extends Controller
{
protected $repository;
public function __construct(CategoryRepository $repository)
{
$this->repository = $repository;
}
...
}
$results = $this->repository->all();
$result = $this->repository->where([['abc', '!=', 'def']])->first(); // Fire ModelNotFoundException (firstOrFail)
$result = $this->repository->findBy('abc', 'def')->first(['*'], false); // Not fire ModelNotFoundException (first)
namespace App\Repositories\Criteria;
use GuilhermeGonzaga\Repository\Contracts\RepositoryContract as Repository;
use GuilhermeGonzaga\Repository\Criteria\Criteria;
class PopularProducts extends Criteria
{
public function apply(Repository $repository)
{
$repository->where([
['purchases', '>', '50']
]);
}
}
namespace App\Repositories\Criteria;
use GuilhermeGonzaga\Repository\Contracts\RepositoryContract as Repository;
use GuilhermeGonzaga\Repository\Criteria\Criteria;
class ProductsByCategory extends Criteria
{
protected $category;
public function __construct($category)
{
$this->category = $category;
}
public function apply(Repository $repository)
{
$repository->findBy('category_id', $this->category);
}
}
use App\Repositories\ProductRepository;
use App\Repositories\Criteria\PopularProducts;
use App\Repositories\Criteria\ProductsByCategory;
use App\Repositories\Criteria\ProductsByCategories;
class ProductsController extends Controller
{
protected $repository;
public function __construct(ProductRepository $repository)
{
$this->repository = $repository;
}
public function index()
{
$this->repository->criteria(PopularProducts::class)->get();
}
public function showByCategory($category)
{
$this->repository->criteria(ProductsByCategory::class, [$category])->get();
}
public function showByCategories($category1, $category2)
{
$this->repository->criteria(ProductsByCategories::class, [
$category1,
$category2,
$category3
])->get();
}
}