Downloads
Stars
Version
- l5-repository simple
Execute the following command to get the latest version of the package:
composer require herojhc/laravel-repository
ServiceProvider will be attached automatically
In your config/app.php
add Herojhc\Repositories\Providers\RepositoryServiceProvider::class
to the end of the providers
array:
'providers' => [
...
Herojhc\Repositories\Providers\RepositoryServiceProvider::class,
],
If Lumen
$app->register(Herojhc\Repositories\Providers\LumenRepositoryServiceProvider::class);
Publish Configuration
php artisan vendor:publish --provider "Herojhc\Repositories\Providers\RepositoryServiceProvider"
Create your model normally, but it is important to define the attributes that can be filled from the input form data.
namespace App;
class Post extends Eloquent { // or Ardent, Or any other Model Class
protected $fillable = [
'title',
'author',
...
];
...
}
namespace App;
use Herojhc\Repositories\Eloquent\BaseRepository;
class PostRepository extends BaseRepository {
/**
* Specify Model class name
*
* @return string
*/
function model()
{
return "App\\Post";
}
}
php artisan make:criteria My
Criteria are a way to change the repository of the query by applying specific conditions according to your needs. You can add multiple Criteria in your repository.
use Herojhc\Repositories\Contracts\RepositoryInterface;
use Herojhc\Repositories\Criteria\Criteria;
class MyCriteria extend Criteria {
public function apply($model, RepositoryInterface $repository)
{
$model = $model->where('user_id','=', Auth::user()->id );
return $model;
}
}
namespace App\Http\Controllers;
use App\PostRepository;
class PostsController extends BaseController {
/**
* @var PostRepository
*/
protected $repository;
public function __construct(PostRepository $repository){
$this->repository = $repository;
}
public function index()
{
$this->repository->pushCriteria(new MyCriteria1());
$this->repository->pushCriteria(MyCriteria2::class);
$posts = $this->repository->all();
...
}
}