PHP code example of masterkey / repository

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

    

masterkey / repository example snippets




class MyController { 

    protected $user;
    
    public function __construct(\App\Repositories\UserRepository $user)
    {
        $this->user = $user;
    }
    
    public function index()
    {
        return $this->user->all(['column_a', 'column_b']);
    }
}



namespace App\Repositories\Criteria\Movies;

use Masterkey\Repository\Criteria;
use Masterkey\Repository\Contracts\RepositoryContract as Repository;

/**
 * MoviesNotRated
 *
 * @package App\Repositories\Criteria\Movies
 */
class MoviesNotRated extends Criteria
{
    /**
     * @param   $model
     * @param   Repository $repository
     * @return  mixed
     */
    public function apply($model, Repository $repository)
    {
        return $model->where('was_rated', false);
    }
}



use App\Repositories\Criteria\Movies\MoviesNotRated;
use App\Repositories\FilmRepository as Film;

class FilmsController extends Controller {

    /**
     * @var Film
     */
    private $film;

    public function __construct(Film $film)
    {
        $this->film = $film;
    }

    public function index()
    {
        $this->film->pushCriteria(new MoviesNotRated());
        return \Response::json($this->film->all());
    }

    /*
     * Você também pode utilizar o método getByCriteria
     */
    public function notRated()
    {
        $criteria = new MoviesNotRated();
        return $this->film
                    ->getByCriteria($criteria)
                    ->all();
    }
}
sh
$ php artisan vendor:publish
sh
php artisan make:repository UsersRepository --model=Users
# ou ainda
php artisan make:repository Users/Users --model=Models/Users
sh
php artisan make:criteria MoviesNotRated --model=Movie