PHP code example of sebastian-berc / repositories
1. Go to this page and download the library: Download sebastian-berc/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/ */
sebastian-berc / repositories example snippets php
class UsersController extends Controller
{
/**
* Contains users repository.
*
* @var UsersRepository
*/
protected $repository;
/**
* Creates a new instance of users controller.
*
* @param UsersRepository $repository
*/
public function __construct(UsersRepository $repository)
{
$this->repository = $repository;
}
}
php
/**
* Creates a new users.
*/
public function store(Request $request)
{
$repository = UsersRepository::instance();
return $repository->create($request->all());
}
php
// Definition:
$repository->all(array $columns = ['*']);
// Example:
$users = $repository->all(['name', 'value']);
php
// Definition:
$repository->where($column, $operator = '=', $value = null, $boolean = 'and', array $columns = ['*']);
// Example:
$repository->where('id', '<>', \Auth::user()->getKey(), 'and', ['activated', 'banned']);
php
// Definition:
$repository->paginate($perPage = 15, array $columns = ['*']);
// Example:
$repository->paginate(50, ['name', 'value']);
php
// Definition:
$repository->create(array $attributes = []);
// Example:
$repository->create(['activated' => true, 'banned' => false]);
php
// Definition:
$repository->update($identifier, array $attributes = []);
// Example:
$repository->update(1, ['activated' => true, 'banned' => false]);
php
// Definition:
$repository->update($dirtyModel);
// Example:
$model = $repository->find(1);
$model->activated = true;
$repository->update($model);
php
// Definition:
$repository->update($dirtyModel, ['activated' => true]);
// Example:
$model = $repository->find(1);
$model->activated = true;
$repository->update($model, ['banned' => false]);
php
// Definition:
$repository->delete($identifier);
// Example:
$repository->delete(1);
php
// Definition:
$repository->find($identifier, array $columns = ['*']);
// Example:
$repository->find(1, ['name', 'value']);
php
// Definition:
$repository->findBy($column, $value, array $columns = ['*']);
// Example:
$repository->findBy('activated', true, ['id']);
php
// Definition:
$repository->findWhere(array $wheres, array $columns = ['*']);
// Example:
$repository->findWhere(['activated' => true, 'banned' => false], ['id']);
php
// Definition and example:
$repository->count();
php
// Definition:
$repository->fetch($page = 1, $perPage = 15, array $columns = ['*'], array $filter = [], array $sort = []);
// Example:
$repository->fetch(1, 15, ['*'], ['activated' => true, 'banned' => 'false'], ['id' => 'ASC']);
php
// Definition:
$repository->simpleFetch($page = 1, $perPage = 15, array $columns = ['*'], array $filter = [], array $sort = []);
// Example:
$repository->simpleFetch(1, 15, ['*'], ['activated' => true, 'banned' => 'false'], ['id' => 'ASC']);
php
// Definition:
$repository->with($relations);
// Examples:
$repository->with('roles')->all();
$repository->with('roles', 'permissions')->all();
$repository->with(['roles', 'permissions])->all();
php
class ActivatedAndNotBanned extends \SebastianBerc\Repositories\Criteria
{
public function execute(Builder $query)
{
return $query->where(['activated' => true])->andWhere(['banned' => false]);
}
}
php
$repository->criteria(new ActivatedAndNotBanned())->all();
php
class UsersForListing extends \SebastianBerc\Repositories\Transformer
{
public function transform($item)
{
$item->fullname = $item->firstName . ' ' . $item->lastName;
$item->password = '*****';
return $item;
}
}
php
$repository->setTransformer(UsersForListing::class)->all();