1. Go to this page and download the library: Download vydev/easy-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/ */
vydev / easy-repository example snippets
/*
* Package Service Providers...
*/
VyDev\Providers\RepositoryServiceProvider::class
use VyDev\Commands\MakeCriteria;
use VyDev\Commands\MakeRepository;
protected $commands = [
MakeCriteria::class,
MakeRepository::class
];
artisan optimize:clear
namespace App\Repositories\Eloquent;
use VyDev\Repositories\Eloquent\BaseRepository;
use VyDev\Repositories\Criteria\AdvancedSearchCriteria;
class UserRepository extends BaseRepository
{
protected $transform = false;
public function model()
{
return 'App\\User';
}
public function boot()
{
/* Uncomment if you want to use advanced search */
// $this->pushCriteria(new AdvancedSearchCriteria());
}
public function transform($model)
{
return [
'id' => [
'field' => 'id',
'value' => $model->id,
'visible' => true
]
//
];
}
/* You can define your customize function bellow */
public function countPosts()
{
return $this->withCount('posts');
}
}
namespace App\Http\Controllers\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repositories\Eloquent\UserRepository as User;
class IndexController extends Controller
{
protected $user;
public function __construct(User $user)
{
$this->user = $user;
}
public function index()
{
$users = $this->user->all()->export();
$firstUserPosts = $this->user->with('posts')->get()->export();
/* If you want set cache in range of a time */
$cacheKey = 'first-user-posts';
$time = 10;
$firstUserPosts = $this->user->with('posts')->get()->exportWithCache($cacheKey,$time);
/* Set cache forever */
$firstUserPosts = $this->user->with('posts')->get()->exportWithCache($cacheKey); // Set only cache key
}
}
public function all();
public function get($columns = ['*']);
public function first();
public function latest();
public function random();
public function exists();
public function find($id);
public function findOrFail($id);
public function findOrNew($id, $columns = ['*']);
public function pluck($columns,$key);
public function sync($attributes);
public function syncWithoutDetaching($attributes);
public function attach($attributes);
public function detach($attributes);
public function count();
public function firstOrNew(array $attributes);
public function firstOrCreate(array $attributes);
public function limit($arg);
public function take($arg);
public function offset($arg);
public function paginate($limit = 15, $columns = ['*']);
public function where($field,$operator, $value = null);
public function whereIn($field,$values);
public function whereNotIn($field,$values);
public function whereBetween($field,$values);
public function create($values);
public function save();
public function update($values);
public function delete();
public function updateOrCreate(array $attributes, array $values = []);
public function has($relation);
public function with($relations);
public function withCount($relations);
public function loadCount($relations);
public function whereHas($relation, $closure);
public function orderBy($column, $direction = 'asc');
public function load($relation);
public function search($fields,$value);
public function hidden($columns = ['*']);
public function visible($columns = ['*']);
public function increment($field, $quantity = null);
public function decrement($field, $quantity = null);
public function export();
namespace App\Repositories\Criteria\User;
use VyDev\Repositories\Contracts\RepositoryInterface;
use VyDev\Repositories\Criteria\Criteria;
class UserActive extends Criteria
{
public function apply($model, RepositoryInterface $repository)
{
$model = $model->where('active',1);
return $model;
}
}
namespace App\Http\Controllers\User;
use App\Repositories\Criteria\UserActive;
public function testPushCriteria()
{
/* Uncomment to view the change ^^ */
$this->user->pushCriteria(new UserActive());
// return $this->user->all();
$this->user->skipCriteria();
// return $this->user->all();
}
public function testGetByCriteria()
{
/* If you only want to load a specific Criteria, let do this */
return $this->user->getByCriteria(new UserActive());
}
public function testRemoveCriteria()
{
/* If you have 2 or more criteria and you want to remove one of theme */
$this->user->pushManyCriterias(new UserActive(),new UserHaveFivePosts());
/* Delete a specific criteria */
$this->user->popCriteria(new UserHaveFivePosts());
return $this->user->all()->export();
}
public function pushWithCondition(Request $request)
{
$condition = $request->status == 'active' ? true : false;
$this->user->pushCriteriaWhen([
$condition => new FilterActiveUser(),
// Somethings else
]);
}
public function applyCriteria();
public function pushCriteria(Criteria $criteria);
public function pushCriteriaWhen($arguments);
public function pushManyCriterias(...$criterias);
public function skipCriteria();
public function getByCriteria(Criteria $criteria);
public function popCriteria(Criteria $criteria);
public function pushCriteriaWhen($arguments);
public function popManyCriterias(...$criterias);
protected $transform = true; // Define true to use transfomer
public function transform($model)
{
return [
'id' => [
'field' => 'id', // The id field in users table
'value' => $model->id // The new data for id field, Example : value => $model->id + 10,
'visible' => true // Show or hidden, if you delete this key, default value is true
]
// Some...
];
}
use VyDev\Repositories\Criteria\AdvancedSearchCriteria;
public function boot()
{
$this->pushCriteria(new AdvancedSearchCriteria());
}