PHP code example of dugajean / repositories

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

    

dugajean / repositories example snippets


 

namespace App\Http\Controllers;

use App\Repositories\FilmRepository;

class FilmsController extends Controller {

    /**
     * @var FilmRepository 
     */
    private $filmRepository;

    public function __construct(FilmRepository $filmRepository) 
    {
        $this->filmRepository = $filmRepository;
    }

    public function index() 
    {
        return response()->json($this->filmRepository->all());
    }
}

public function all($columns = ['*'])
public function lists($value, $key = null)
public function paginate($perPage = 1, $columns = ['*'], $method = 'full');
public function create(array $data)
// if you use mongodb then you'll need to specify primary key $attribute
public function update(array $data, $id, $attribute = 'id')
public function delete($id)
public function find($id, $columns = ['*'])
public function findBy($field, $value, $columns = ['*'])
public function findAllBy($field, $value, $columns = ['*'])
public function findWhere($where, $columns = ['*'])

public function apply($model, Repository $repository)

$this->filmRepository->create(Input::all());

$this->filmRepository->update(Input::all(), $film_id);

$this->filmRepository->delete($id);

$this->filmRepository->find($id);

$this->filmRepository->find($id, ['title', 'description', 'release_date']);

$this->filmRepository->findBy('title', $title);

$this->filmRepository->findAllBy('author_id', $author_id);

$this->filmRepository->findWhere([
    'author_id' => $author_id,
    ['year', '>', $year]
]);

 

namespace App\Repositories\Criteria\Films;

use Dugajean\Repositories\Criteria\Criteria;
use Dugajean\Repositories\Contracts\RepositoryInterface;

class LengthOverTwoHours extends Criteria 
{
    /**
     * @param $model
     * @param RepositoryInterface $repository
     *                                       
     * @return Model
     */
    public function apply($model, RepositoryInterface $repository)
    {
        return $model->where('length', '>', 120);
    }
}

 

namespace App\Http\Controllers;

use App\Repositories\FilmRepository;
use App\Repositories\Criteria\Films\LengthOverTwoHours;

class FilmsController extends Controller 
{
    /**
     * @var FilmRepository
     */
    private $filmRepository;

    public function __construct(FilmRepository $filmRepository) 
    {
        $this->filmRepository = $filmRepository;
    }

    public function index() 
    {
        $this->filmRepository->pushCriteria(new LengthOverTwoHours());
        
        return response()->json($this->filmRepository->all());
    }
}
bash
php artisan make:repository Film
bash
php artisan vendor:publish --provider="Dugajean\Repositories\Providers\RepositoryProvider"
bash
php artisan make:criteria LengthOverTwoHours --model=Film