PHP code example of giordanolima / eloquent-repository
1. Go to this page and download the library: Download giordanolima/eloquent-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/ */
giordanolima / eloquent-repository example snippets
namespace App\Repositories;
use GiordanoLima\EloquentRepository\BaseRepository;
class UserRepository extends BaseRepository
{
protected function model() {
return \App\User::class;
}
}
namespace App\Repositories;
use GiordanoLima\EloquentRepository\BaseRepository;
class UserRepository extends BaseRepository
{
protected function model() {
return \App\User::class;
}
public function getAllUser(){
return $this->all();
}
public function getByName($name) {
return $this->where("name", $name)->get();
}
// You can create methods with partial queries
public function filterByProfile($profile) {
return $this->where("profile", $profile);
}
// Them you can use the partial queries into your repositories
public function getAdmins() {
return $this->filterByProfile("admin")->get();
}
public function getEditors() {
return $this->filterByProfile("editor")->get();
}
// You can also use Eager Loading in queries
public function getWithPosts() {
return $this->with("posts")->get();
}
}
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Repositories\UserRepository;
class UserController extends Controller
{
protected function index(UserRepository $repository) {
return $repository->getAdmins();
}
}
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Repositories\UserRepository;
class UserController extends Controller
{
private $repository;
public function __construct()(UserRepository $repository) {
$this->repository = $repository;
}
public function index() {
return $this->repository->getAllUsers();
}
}
namespace App\Repositories;
use GiordanoLima\EloquentRepository\BaseRepository;
class UserRepository extends BaseRepository
{
protected $perPage = 10;
protected function model() {
return \App\User::class;
}
}
namespace App\Repositories;
use GiordanoLima\EloquentRepository\BaseRepository;
class UserRepository extends BaseRepository
{
protected $orderBy = "created_at";
protected $orderByDirection = "DESC";
protected function model() {
return \App\User::class;
}
public function getAllUser(){
// This query will use the default ordering of the repository.
return $this->all();
}
public function getByName($name) {
// In this query only the declared sort will be used.
return $this->orderBy("name")->where("name", $name)->get();
}
// É possível criar métodos com consultas parciais
public function getWithoutOrder() {
// In this query, no sort will be used.
return $this->skipOrderBy()->all();
}
}
namespace App\Repositories;
use GiordanoLima\EloquentRepository\BaseRepository;
class AdminRepository extends BaseRepository
{
protected function model() {
return \App\User::class;
}
protected function globalScope() {
return $this->where('is_admin', true);
}
public function getAdmins() {
// In this query the declared global scope will be
namespace App\Repositories;
use GiordanoLima\EloquentRepository\BaseRepository;
use GiordanoLima\EloquentRepository\CacheableRepository;
class UserRepository extends BaseRepository
{
use CacheableRepository;
protected function model() {
return \App\User::class;
}
}
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Repositories\UserRepository;
class UserController extends Controller
{
private $repository;
public function __construct()(UserRepository $repository) {
$this->repository = $repository;
}
public function index() {
$users = $this->repository->getAllUsers();
// if you call the same query later (even in other requests)
// the query is already cached and you do not need to access the database again.
$users = $this->repository->getAllUsers();
}
}
namespace App\Repositories;
use GiordanoLima\EloquentRepository\BaseRepository;
use GiordanoLima\EloquentRepository\CacheableRepository;
class UserRepository extends BaseRepository
{
use CacheableRepository;
protected function model() {
return \App\User::class;
}
public function createUser($data) {
// After inserting the data, the cache
// is cleaned automatically.
return $this->create($data);
}
public function addRules($user, $rules) {
$user->rules()->attach($rules);
$this->clearCache(); // Forcing cache cleaning.
}
public function getWithoutCache() {
$this->skipCache()->all(); // Finding the data without using the cache.
}
}
shell
php artisan vendor:publish
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.