This package allows you to create Model Repository with ease. You can use this package as an add-on on existing laravel model classes. Hightlights are given below
- Cache Repository
- Model Repository
- Make Repository Artisan Commands
Run the following command:
composer require laravelutilities/repository
Publish config file.
php artisan vendor:publish --tag=repository
You can change the options of your app from config/repository.php
file
You can inject the dependency of a repository in any controller and use its functions like $this->repository->fetchByCreatedAt('foo')
or if you want to use cache $this->repository->fetchByCreatedAt('foo', 'cache')
$this->repository->findByFieldFromCache('created_at', 'somevalue'); // extending cache repository; fetch only single value
$this->repository->findByFieldsFromCache(['created_at' => 'somevalue', 'city' => 'someanothervalue',...]); // fetch multiple values
$this->repository->findByField('created_at', 'somevalue'); // extending model repository; fetch only single value
$this->repository->findByField('created_at', 'somevalue'); // extending model repository; fetch only single value
public function fetchByFieldFromCache($key, $value);
public function fetchByFieldsFromCache(array $fieldsAndValues);
public function getById($id);
public function getByIds(array $ids)
use App\Repositories\OrganizationRepository;
protected $organization;
public function __construct()
{
$this->organization = new OrganizationRepository();
}
namespace App\Repositories;
use App\Models\AppLog;
use LaravelUtility\Repository\Repositories\ModelRepository;
class AppLogRepository extends ModelRepository
{
public function __construct()
{
parent::__construct(new AppLog());
}
}
namespace App\Repositories;
use App\Models\AppLog;
use LaravelUtility\Repository\Repositories\CacheRepository;
class AppLogRepository extends CacheRepository
{
public function __construct()
{
parent::__construct(new AppLog());
}
}
public function findByField($key, $value);
public function findByFields(array $fieldsAndValues);
public function fetchByField($key, $value);
public function fetchByFields(array $fieldsAndValues);
public function findByFieldFromCache($key, $value);
public function findByFieldsFromCache(array $fieldsAndValues);
public function fetchByFieldFromCache($key, $value);
public function fetchByFieldsFromCache(array $fieldsAndValues);
public function getById($id);
public function getByIds(array $ids)
A Trait has been added to further augment these functions using magic method. In all the above mentioned functions, Field
can be replaced in any of the model field. Suppose, you have a field created_at
inside your table; this can be called in variaous ways given below
$this->repository->findByCreatedAt('somevalue'); // extending model repository; fetch only single value
$this->repository->findByCreatedAt('somevalue','cache'); // extending cache repository; fetch only single value
$this->repository->fetchByCreatedAt(['created_at' => 'somevalue', 'city' => 'someanothervalue',...]); // fetch multiple values
php artisan make:repository <name> --model=<model> --cache=true/false
You can create Repository classes using make:repository
command.
<name>
is mandatory; specifies the name of the repository. Using Repository
as a suffix is mandatory. And if you don't write the name with Repository
as suffix, command do for you.
--model=modelname
as option if you don't menthion the name of the model, it tries to find the model same as Repository .if model is not created it ask if you want to creat new model.
Please see Releases for more information what has changed recently.
The MIT License (MIT). Please see LICENSE for more information.