PHP code example of deva7mad / laravel-repositories
1. Go to this page and download the library: Download deva7mad/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/ */
deva7mad / laravel-repositories example snippets
namespace App\Repositories;
use Rinvex\Repository\Repositories\EloquentRepository;
class FooRepository extends EloquentRepository
{
protected $repositoryId = 'rinvex.repository.uniqueid';
protected $model = 'App\Models\User';
}
namespace App\Repositories;
use Illuminate\Contracts\Container\Container;
use Rinvex\Repository\Repositories\EloquentRepository;
class FooRepository extends EloquentRepository
{
// Instantiate repository object with
namespace App\Http\Controllers;
use App\Repositories\FooRepository;
class BarController
{
// Inject `FooRepository` from the IoC
public function baz(FooRepository $repository)
{
// Find entity by primary key
$repository->find(1);
// Find all entities
$repository->findAll();
// Create a new entity
$repository->create(['name' => 'Example']);
}
}
return [
/*
|--------------------------------------------------------------------------
| Models Directory
|--------------------------------------------------------------------------
|
| Here you may specify the default models directory, just write
| directory name, like 'Models' not the full path.
|
| Default: 'Models'
|
*/
'models' => 'Models',
/*
|--------------------------------------------------------------------------
| Caching Strategy
|--------------------------------------------------------------------------
*/
'cache' => [
/*
|--------------------------------------------------------------------------
| Cache Keys File
|--------------------------------------------------------------------------
|
| Here you may specify the cache keys file that is used only with cache
| drivers that does not support cache tags. It is mandatory to keep
| track of cache keys for later usage on cache flush process.
|
| Default: storage_path('framework/cache/rinvex.repository.json')
|
*/
'keys_file' => storage_path('framework/cache/rinvex.repository.json'),
/*
|--------------------------------------------------------------------------
| Cache Lifetime
|--------------------------------------------------------------------------
|
| Here you may specify the number of minutes that you wish the cache
| to be remembered before it expires. If you want the cache to be
| remembered forever, set this option to -1. 0 means disabled.
|
| Default: -1
|
*/
'lifetime' => -1,
/*
|--------------------------------------------------------------------------
| Cache Clear
|--------------------------------------------------------------------------
|
| Specify which actions would you like to clear cache upon success.
| All repository cached data will be cleared accordingly.
|
| Default: ['create', 'update', 'delete']
|
*/
'clear_on' => [
'create',
'update',
'delete',
],
/*
|--------------------------------------------------------------------------
| Cache Skipping URI
|--------------------------------------------------------------------------
|
| For testing purposes, or maybe some certain situations, you may wish
| to skip caching layer and get fresh data result set just for the
| current request. This option allows you to specify custom
| URL parameter for skipping caching layer easily.
|
| Default: 'skipCache'
|
*/
'skip_uri' => 'skipCache',
],
];
// Set the IoC container instance
$repository->setContainer(new \Illuminate\Container\Container());
// Get the IoC container instance
$container = $repository->getContainer();
// Set the connection associated with the repository
$repository->setConnection('mysql');
// Get the current connection for the repository
$connection = $repository->getConnection();
// Set the repository model
$repository->setModel(\App\Models\User::class);
// Get the repository model
$repositoryModel = $repository->getModel();
// Set the repository identifier
$repository->setRepositoryId('rinvex.repository.uniqueid');
// Get the repository identifier
$repositoryId = $repository->getRepositoryId();
// Set the repository cache lifetime
$repository->setCacheLifetime(123);
// Get the repository cache lifetime
$cacheLifetime = $repository->getCacheLifetime();
// Set the repository cache driver
$repository->setCacheDriver('redis');
// Get the repository cache driver
$cacheDriver = $repository->getCacheDriver();
// Pass a string
$repository->with('relationship');
// Or an array
$repository->with(['relationship1', 'relationship2']);
$repository->where('slug', '=', 'example');
$repository->whereIn('id', [1, 2, 5, 8]);
$repository->whereNotIn('id', [1, 2, 5, 8]);
use Illuminate\Database\Eloquent\Builder;
$repository->whereHas('attachments', function (Builder $builder) use ($attachment) {
$builder->where('attachment_id', $attachment->id);
});
use Illuminate\Database\Eloquent\Builder;
$entities = $repository->findWhereHas(['attachments', function (Builder $builder) use ($attachment) {
$builder->where('attachment_id', $attachment->id);
}]);
class FilesystemRepository extends BaseRepository
{
// Implement here all `RepositoryContract` methods that query/persist data to & from filesystem or whatever datastore
}
// Set cache lifetime for this individual query to 123 minutes
$repository->setCacheLifetime(123);
// Set cache lifetime for this individual query to forever
$repository->setCacheLifetime(-1);
// Disable cache for this individual query
$repository->setCacheLifetime(0);
// Set cache driver for this individual query to redis
$repository->setCacheDriver('redis');
// Change cache lifetime & driver on runtime
$repository->setCacheLifetime(123)->setCacheDriver('redis')->findAll();
// Use default cache lifetime & driver
$repository->findAll();