PHP code example of prettus / laravel-repository

1. Go to this page and download the library: Download prettus/laravel-repository 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/ */

    

prettus / laravel-repository example snippets


    'Prettus\Repository\RepositoryServiceProvider',

class Post extends Eloquent { // or Ardent, Or any other Model Class

    protected $fillable = [
        'title',
        'author',
        ...
     ];

     ...
}

use Prettus\Repository\Eloquent\Repository;

class PostRepository extends Repository {

    public function __construct(Post $model)
    {
        parent::__construct($model);
    }   
    
}

class PostsController extends BaseController {

    /**
     * @var PostRepository
     */
    protected $repository;

    public function __construct(PostRepository $repository){
        $this->repository = $repository;
    }
    
    ....
}

$posts = $this->repository->all();

$posts = $this->repository->paginate($limit = null, $columns = ['*']);

$post = $this->repository->find($id);

$post = $this->repository->create( Input::all() );

$post = $this->repository->update( Input::all(), $id );

$this->repository->delete($id)

class MyCriteria implements \Prettus\Repository\Contracts\Criteria {

    public function apply($query)
    {
        $query = $query->where('user_id','=', Auth::user()->id );
        return $query;
    }
}


class PostsController extends BaseController {

    /**
     * @var PostRepository
     */
    protected $repository;

    public function __construct(PostRepository $repository){
        $this->repository = $repository;
    }


    public function index()
    {
        $this->repository->pushCriteria(new MyCriteria());
        $posts = $this->repository->all();
		...
    }

}

$posts = $this->repository->getByCriteria(new MyCriteria());

use Prettus\Repository\Eloquent\Repository;

class PostRepository extends Repository {

    public function __construct(Post $model)
    {
        parent::__construct($model);
    }
    
    public function boot(){
        $this->pushCriteria(new MyCriteria());
        $this->pushCriteria(new AnotherCriteria());
        ...
    }
    
}


$posts = $this->repository->skipCriteria()->all();


use Prettus\Repository\Eloquent\Repository;
use Prettus\Repository\Criteria\RequestCriteria;

class PostRepository extends Repository {

	/**
     * @var array
     */
    protected $fieldSearchable = [
        'name',
        'email'
    ];

    public function __construct(Post $model)
    {
        parent::__construct($model);
    }
    
    public function boot(){
        $this->pushCriteria(app('Prettus\Repository\Criteria\RequestCriteria'));
        ...
    }
    
}

protected $fieldSearchable = [
	'name',
	'email'
];

protected $fieldSearchable = [
	'name'=>'like',
	'email', // Default Condition "="
	'your_field'=>'condition'
];

	public function index()
    {
        $this->repository->pushCriteria(app('Prettus\Repository\Criteria\RequestCriteria'));
        $posts = $this->repository->all();
		...
    }
shell
php artisan config:publish prettus/laravel-repository
json
[
    {
        "id": 1,
        "name": "Anderson Andrade",
        "email": "[email protected]",
        "created_at": "-0001-11-30 00:00:00",
        "updated_at": "-0001-11-30 00:00:00"
    }
]