Package Data | |
---|---|
Maintainer Username: | noitran |
Maintainer Contact: | noitran.black@gmail.com (Noitran.Black) |
Package Create Date: | 2019-02-07 |
Package Last Update: | 2020-05-30 |
Language: | PHP |
License: | LGPL-3.0 |
Last Refreshed: | 2024-11-15 15:10:03 |
Package Statistics | |
---|---|
Total Downloads: | 1 |
Monthly Downloads: | 0 |
Daily Downloads: | 0 |
Total Stars: | 0 |
Total Watchers: | 1 |
Total Forks: | 0 |
Total Open Issues: | 0 |
$ composer require noitran/micro-repositories
$ artisan vendor:publish --provider="Noitran\Repositories\ServiceProvider"
$app->register(Noitran\Repositories\ServiceProvider::class);
$app->configure('repositories');
<?php
namespace App\Data\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
class User extends Model
{
/**
* The attributes that aren't mass assignable.
*
* @var array
*/
protected $guarded = [
'id',
];
}
<?php
namespace App\Data\Repositories\User;
use Noitran\Repositories\Contracts\Repository\RepositoryInterface;
/**
* Interface UserRepository
*/
interface UserRepository extends RepositoryInterface
{
//
}
SqlRepository
which adds support for querying with table names. If you use jenssegers/laravel-mongodb
package, then your repository implementation should extend MongoRepository
class<?php
namespace App\Data\Repositories\User;
use Noitran\Repositories\Repositories\SqlRepository;
use App\Data\Models\User;
/**
* Class UserRepositoryEloquent
*/
class UserRepositoryEloquent extends SqlRepository implements UserRepository
{
/**
* Specify Model class name.
*
* @return string
*/
public function getModelClassName(): string
{
return User::class;
}
/**
* Boot up the repository, pushing criteria.
*/
public function boot(): void
{
//
}
}
App\Providers\AppServiceProvider
class.<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\Data\Repositories\User\UserRepository;
use App\Data\Repositories\User\UserRepositoryEloquent;
class AppServiceProvider extends ServiceProvider
{
/**
* Register any application services.
*
* @return void
*/
public function register(): void
{
$this->app->bind(UserRepository::class, UserRepositoryEloquent::class);
}
}
Filters are sitting on top of Repositories and uses Criteria to enable query filtering abilities with built in and / or custom Criteria.
Creating filter class is easy. Example:
<?php
namespace App\Data\Filters\User;
use Noitran\Repositories\Filters\AbstractFilter;
use App\Data\Repositories\User\UserRepository;
class UserFilter extends AbstractFilter
{
/**
* @var array
*/
protected $queryFilters = [
// List of custom Criteria filters
];
/**
* UserFilter constructor.
*
* @param UserRepository $userRepository
*/
public function __construct(UserRepository $userRepository)
{
parent::__construct();
$this->setRepository($userRepository)
->setQueryFilters($this->queryFilters);
}
/**
* @param array $requestAttributes
*
* @return mixed
*/
public function filter(array $requestAttributes)
{
$input = $this->getInput(
$this->queryFilters,
$requestAttributes
);
$this->repository = $this->pushFilters($this->repository, $input);
return $this;
}
}
@todo