{"_id":"57792a2fb035d60e00b12e47","version":{"_id":"563b65bd9e3f2225009fd2bf","project":"563b65bd9e3f2225009fd2bc","__v":4,"createdAt":"2015-11-05T14:20:45.639Z","releaseDate":"2015-11-05T14:20:45.639Z","categories":["563b65be9e3f2225009fd2c0","563b6b25e951f60d000b4513","563c239e260dde0d00c5e890","563c2440260dde0d00c5e891"],"is_deprecated":false,"is_hidden":false,"is_beta":true,"is_stable":true,"codename":"","version_clean":"4.0.0","version":"4.0"},"parentDoc":null,"__v":1,"project":"563b65bd9e3f2225009fd2bc","user":"563b65409e3f2225009fd2b9","category":{"_id":"563b6b25e951f60d000b4513","version":"563b65bd9e3f2225009fd2bf","__v":6,"pages":["563c21fd19ae7b0d0050d45b","563c220c7539dd0d00dbee87","563c2218ac77910d00279fe7","563c2233d8f2d20d00448b4f","563c2376913e650d00b65dbd","563c907319ae7b0d0050d528"],"project":"563b65bd9e3f2225009fd2bc","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2015-11-05T14:43:49.565Z","from_sync":false,"order":1,"slug":"the-basics","title":"The Basics"},"updates":[],"next":{"pages":[],"description":""},"createdAt":"2016-07-03T15:07:27.873Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"settings":"","auth":"required","params":[],"url":""},"isReference":false,"order":6,"body":"Litepie Repository is used to abstract the data layer. This allows the user to create a more flexible application and further providing an ease of maintenance on the same. \n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Repository Interface\"\n}\n[/block]\nThe Link to the same can be found under\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"Litepie\\\\Repository\\\\Contracts\\\\RepositoryInterface\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nThe following methods are present under the Repository Interface\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"all($columns = array('*'))\\nfirst($columns = array('*'))\\npaginate($limit = null, $columns = ['*'])\\nfind($id, $columns = ['*'])\\nfindByField($field, $value, $columns = ['*'])\\nfindWhere(array $where, $columns = ['*'])\\nfindWhereIn($field, array $where, $columns = [*])\\nfindWhereNotIn($field, array $where, $columns = [*])\\ncreate(array $attributes)\\nupdate(array $attributes, $id)\\ndelete($id)\\nwith(array $relations);\\nhidden(array $fields);\\nvisible(array $fields);\\nscopeQuery(Closure $scope);\\ngetFieldsSearchable();\\nsetPresenter($presenter);\\nskipPresenter($status = true);\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Repository Criteria Interface\"\n}\n[/block]\nThe Link to the same can be found under\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"Litepie\\\\Repository\\\\Contracts\\\\RepositoryCriteriaInterface\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nThe following methods are present under the Repository Criteria Interface\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"pushCriteria(CriteriaInterface $criteria)\\ngetCriteria()\\ngetByCriteria(CriteriaInterface $criteria)\\nskipCriteria($status = true)\\ngetFieldsSearchable()\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Cacheable Interface\"\n}\n[/block]\nThe Link to the same can be found under\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"Litepie\\\\Repository\\\\Contracts\\\\CacheableInterface\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nThe following methods are present under the Cacheable Interface\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"setCacheRepository(CacheRepository $repository)\\ngetCacheRepository()\\ngetCacheKey($method, $args = null)\\ngetCacheMinutes()\\nskipCache($status = true)\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Presenter Interface\"\n}\n[/block]\nThe Link to the same can be found under\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"Litepie\\\\Repository\\\\Contracts\\\\PresenterInterface\",\n      \"language\": \"php\",\n      \"name\": null\n    }\n  ]\n}\n[/block]\nThe following methods are present under the Cacheable Interface\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"present($data);\",\n      \"language\": \"php\",\n      \"name\": null\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Presentable\"\n}\n[/block]\nThe Link to the same can be found under\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"Litepie\\\\Repository\\\\Contracts\\\\Presentable\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nThe following methods are present under Presentable\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"setPresenter(PresenterInterface $presenter);\\npresenter();\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Criteria Interface\"\n}\n[/block]\nThe Link to the same can be found under\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"Litepie\\\\Repository\\\\Contracts\\\\CriteriaInterface\",\n      \"language\": \"php\",\n      \"name\": null\n    }\n  ]\n}\n[/block]\nThe following methods under Criteria interface are as follows\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"apply($model, RepositoryInterface $repository);\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Transformable\"\n}\n[/block]\nThe Link to the same can be found under\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"Litepie\\\\Repository\\\\Contracts\\\\Transformable\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nThe following methods under Tranformable are as follows\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"transform();\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"USAGE\"\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"CREATE A MODEL\"\n}\n[/block]\nThe user can use the code to create a model normally, however it is important to ensure that attributes are defined from the input data.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"namespace App;\\n\\nclass Post extends Eloquent { // or Ardent, Or any other Model Class\\n\\n    protected $fillable = [\\n        'title',\\n        'author',\\n        ...\\n     ];\\n\\n     ...\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"CREATE A REPOSITORY\"\n}\n[/block]\nThe user can develop a Repository for your Database by extending the Base repository.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"namespace App;\\n\\nuse Litepie\\\\Repository\\\\Eloquent\\\\BaseRepository;\\n\\nclass PostRepository extends BaseRepository {\\n\\n    /**\\n     * Specify Model class name\\n     *\\n     * :::at:::return string\\n     */\\n    function model()\\n    {\\n        return \\\"App\\\\\\\\Post\\\";\\n    }\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"GENERATORS\"\n}\n[/block]\nThe Generators have enable users to create Repositories with ease.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Config\"\n}\n[/block]\nThe user must first configure the respective storage location of the repository files. This is by default done through the \"app\" folder and the namespace \"App\".\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"...\\n    'generator'=>[\\n        'basePath'=>app_path(),\\n        'rootNamespace'=>'App\\\\\\\\',\\n        'paths'=>[\\n            'models'=>'Entities',\\n            'repositories'=>'Repositories',\\n            'interfaces'=>'Repositories',\\n            'transformers'=>'Transformers',\\n            'presenters'=>'Presenters'\\n        ]\\n    ]\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nThe user may want to save the root of your project folder outside the location of the app and add another namespace, For Eg.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"...\\n     'generator'=>[\\n        'basePath'      => base_path('src/Lorem'),\\n        'rootNamespace' => 'Lorem\\\\\\\\'\\n    ]\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nAdditionally, the user may wish to customize where your generated classes gets saved. This can be accomplished by editing the **paths **node to your liking. For Eg.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \" 'generator'=>[\\n        'basePath'=>app_path(),\\n        'rootNamespace'=>'App\\\\\\\\',\\n        'paths'=>[\\n            'models'=>'Models',\\n            'repositories'=>'Repositories\\\\\\\\Eloquent',\\n            'interfaces'=>'Contracts\\\\\\\\Repositories',\\n            'transformers'=>'Transformers',\\n            'presenters'=>'Presenters'\\n        ]\\n    ]\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"COMMANDS\"\n}\n[/block]\nIn order to generate all the functions that the user needs for the respective Model, this command can be run:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"php artisan make:entity Post\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nThis will create the Model, the Repository, the Presenter and the Transformer classes. \n\nThe user can also pass the options from the **repository **command, since this command is just a wrapper.\n\nTo generate a repository for your Post model, the following command can be utilized\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"php artisan make:repository Post\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nIf you would like to implement a namespace to your blog, you could utilize the following command.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"php artisan make:repository \\\"Blog\\\\Post\\\"\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nThere are certain Added fields that are fillable by the user.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"php artisan make:repository \\\"Blog\\\\Post\\\" --fillable=\\\"title,content\\\"\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nWhen the user is running commado, he/she will be creating the \"Entities\" folder and \"Repositories\" inside the folder that you set as the default.\n\nDone, done that just now you do bind its interface for your real repository, for example in your own Repositories Service Provider.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"App::bind('{YOUR_NAMESPACE}Repositories\\\\PostRepository', '{YOUR_NAMESPACE}Repositories\\\\PostRepositoryEloquent');\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nAnd use\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"public function __construct({YOUR_NAMESPACE}Repositories\\\\PostRepository $repository){\\n    $this->repository = $repository;\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"USE METHOD\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"namespace App\\\\Http\\\\Controllers;\\n\\nuse App\\\\PostRepository;\\n\\nclass PostsController extends BaseController {\\n\\n    /**\\n     * @var PostRepository\\n     */\\n    protected $repository;\\n\\n    public function __construct(PostRepository $repository){\\n        $this->repository = $repository;\\n    }\\n\\n    ....\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nFind all results in Repository\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"$posts = $this->repository->all();\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nFind all results in Repository with pagination\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"$posts = $this->repository->paginate($limit = null, $columns = ['*']);\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nFind by result by id\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"$post = $this->repository->find($id);\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nHiding attributes of the model\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"$post = $this->repository->hidden(['country_id'])->find($id);\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nShowing only specific attributes of the model\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"$post = $this->repository->visible(['id', 'state_id'])->find($id);\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nLoading the Model relationships\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"$post = $this->repository->with(['state'])->find($id);\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nFind by result by field name\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"$posts = $this->repository->findByField('country_id','15');\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nFind by result by multiple fields\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"$posts = $this->repository->findWhere([\\n    //Default Condition =\\n    'state_id'=>'10',\\n    'country_id'=>'15',\\n    //Custom Condition\\n    ['columnName','>','10']\\n]);\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nFind by result by multiple values in one field\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"$posts = $this->repository->findWhereIn('id', [1,2,3,4,5]);\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nFind by result by excluding multiple values in one field\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"$posts = $this->repository->findWhereNotIn('id', [6,7,8,9,10]);\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nFind all using custom scope\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"$posts = $this->repository->scopeQuery(function($query){\\n    return $query->orderBy('sort_order','asc');\\n})->all();\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nCreate new entry in Repository\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"$post = $this->repository->create( Input::all() );\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nUpdate entry in Repository\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"$post = $this->repository->update( Input::all(), $id );\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nDelete entry in Repository\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"$this->repository->delete($id)\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"CREATE A CRITERIA\"\n}\n[/block]\nThe Criteria is a way to change the repository of the query by applying specific conditions according to the user's needs. The user can add multiple Criteria into the repository.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"use Litepie\\\\Repository\\\\Contracts\\\\RepositoryInterface;\\nuse Litepie\\\\Repository\\\\Contracts\\\\CriteriaInterface;\\n\\nclass MyCriteria implements CriteriaInterface {\\n\\n    public function apply($model, RepositoryInterface $repository)\\n    {\\n        $model = $model->where('user_id','=', Auth::user()->id );\\n        return $model;\\n    }\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"USING THE CRITERIA IN A CONTROLLER\"\n}\n[/block]\nThe criteria can be utilized in the controller using the following code.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"namespace App\\\\Http\\\\Controllers;\\n\\nuse App\\\\PostRepository;\\n\\nclass PostsController extends BaseController {\\n\\n    /**\\n     * @var PostRepository\\n     */\\n    protected $repository;\\n\\n    public function __construct(PostRepository $repository){\\n        $this->repository = $repository;\\n    }\\n\\n\\n    public function index()\\n    {\\n        $this->repository->pushCriteria(new MyCriteria());\\n        $posts = $this->repository->all();\\n    ...\\n    }\\n\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nThe user could obtain the results from Criteria utilizing the following code.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"$posts = $this->repository->getByCriteria(new MyCriteria());\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nThe user can set the default Criteria in Repository\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"use Litepie\\\\Repository\\\\Eloquent\\\\BaseRepository;\\n\\nclass PostRepository extends BaseRepository {\\n\\n    public function boot(){\\n        $this->pushCriteria(new MyCriteria());\\n        $this->pushCriteria(new AnotherCriteria());\\n        ...\\n    }\\n\\n    function model(){\\n       return \\\"App\\\\\\\\Post\\\";\\n    }\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"SKIP CRITERIA\"\n}\n[/block]\nThe user can utilize the **skipCriteria** before any chaining method.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"$posts = $this->repository->skipCriteria()->all();\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"USING THE REQUESTCRITERIA\"\n}\n[/block]\nThe RequestCriteria is a standard Criteria implementation. It enables filters to perform in the repository from parameters sent in the request.\n\nThe user can perform a dynamic search, filter the data and customize the queries.\n\nTo use the Criteria in your repository, the user can add a new criteria in the boot method of your repository, or directly use in your controller, in order to filter out only a few requests.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"ENABLING YOUR REPOSITORY\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"use Litepie\\\\Repository\\\\Eloquent\\\\BaseRepository;\\nuse Litepie\\\\Repository\\\\Criteria\\\\RequestCriteria;\\n\\n\\nclass PostRepository extends BaseRepository {\\n\\n  /**\\n     * @var array\\n     */\\n    protected $fieldSearchable = [\\n        'name',\\n        'email'\\n    ];\\n\\n    public function boot(){\\n        $this->pushCriteria(app('Litepie\\\\Repository\\\\Criteria\\\\RequestCriteria'));\\n        ...\\n    }\\n\\n    function model(){\\n       return \\\"App\\\\\\\\Post\\\";\\n    }\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nThe user would need to define which fields from the model can be searchable.\n\nIn the repository the fields that are searchable should be set to $fieldSearchable.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"protected $fieldSearchable = [\\n  'name',\\n  'email'\\n];\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nYou can set the type of condition which will be used to perform the query, the default condition is **\"=\"** \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"protected $fieldSearchable = [\\n  'name'=>'like',\\n  'email', // Default Condition \\\"=\\\"\\n  'your_field'=>'condition'\\n];\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"ENABLING IN CONTROLLER\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \" public function index()\\n    {\\n        $this->repository->pushCriteria(app('Litepie\\\\Repository\\\\Criteria\\\\RequestCriteria'));\\n        $posts = $this->repository->all();\\n    ...\\n    }\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"EXAMPLE THE CRITERIA\"\n}\n[/block]\nThe user can Request all data without filter by request\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"http://prettus.local/users\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"[\\n    {\\n        \\\"id\\\": 1,\\n        \\\"name\\\": \\\"John Doe\\\",\\n        \\\"email\\\": \\\"john@gmail.com\\\",\\n        \\\"created_at\\\": \\\"-0001-11-30 00:00:00\\\",\\n        \\\"updated_at\\\": \\\"-0001-11-30 00:00:00\\\"\\n    },\\n    {\\n        \\\"id\\\": 2,\\n        \\\"name\\\": \\\"Lorem Ipsum\\\",\\n        \\\"email\\\": \\\"lorem@ipsum.com\\\",\\n        \\\"created_at\\\": \\\"-0001-11-30 00:00:00\\\",\\n        \\\"updated_at\\\": \\\"-0001-11-30 00:00:00\\\"\\n    },\\n    {\\n        \\\"id\\\": 3,\\n        \\\"name\\\": \\\"Laravel\\\",\\n        \\\"email\\\": \\\"laravel@gmail.com\\\",\\n        \\\"created_at\\\": \\\"-0001-11-30 00:00:00\\\",\\n        \\\"updated_at\\\": \\\"-0001-11-30 00:00:00\\\"\\n    }\\n]\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nConducting research in the repository\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"http://prettus.local/users?search=John%20Doe\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n***Or*** \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"http://prettus.local/users?search=John&searchFields=name:like\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n***Or*** \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"http://prettus.local/users?search=john@gmail.com&searchFields=email:=\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n***Or***\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"http://prettus.local/users?search=name:John Doe;email:john@gmail.com\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n***Or***\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"http://prettus.local/users\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"?search=name:John;email:john@gmail.com&searchFields=name:like;email:=\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"[\\n    {\\n        \\\"id\\\": 1,\\n        \\\"name\\\": \\\"John Doe\\\",\\n        \\\"email\\\": \\\"john@gmail.com\\\",\\n        \\\"created_at\\\": \\\"-0001-11-30 00:00:00\\\",\\n        \\\"updated_at\\\": \\\"-0001-11-30 00:00:00\\\"\\n    }\\n]\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nFiltering fields\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"http://prettus.local/users?filter=id;name\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"[\\n    {\\n        \\\"id\\\": 1,\\n        \\\"name\\\": \\\"John Doe\\\"\\n    },\\n    {\\n        \\\"id\\\": 2,\\n        \\\"name\\\": \\\"Lorem Ipsum\\\"\\n    },\\n    {\\n        \\\"id\\\": 3,\\n        \\\"name\\\": \\\"Laravel\\\"\\n    }\\n]\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nSorting the results\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"http://prettus.local/users?filter=id;name&orderBy=id&sortedBy=desc\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"[\\n    {\\n        \\\"id\\\": 3,\\n        \\\"name\\\": \\\"Laravel\\\"\\n    },\\n    {\\n        \\\"id\\\": 2,\\n        \\\"name\\\": \\\"Lorem Ipsum\\\"\\n    },\\n    {\\n        \\\"id\\\": 1,\\n        \\\"name\\\": \\\"John Doe\\\"\\n    }\\n]\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nAdd relationship\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"http://prettus.local/users?with=groups\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"OVERWRITE PARAMS NAME\"\n}\n[/block]\nThe user can change the name of the parameters in the configuration file **config/repository.php**\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"CACHE\"\n}\n[/block]\nThe user can further add a layer of cache easily to your repository\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Cache Usage\"\n}\n[/block]\nImplements the interface CacheableInterface and use CacheableRepository Trait.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"use Litepie\\\\Repository\\\\Eloquent\\\\BaseRepository;\\nuse Litepie\\\\Repository\\\\Contracts\\\\CacheableInterface;\\nuse Litepie\\\\Repository\\\\Traits\\\\CacheableRepository;\\n\\nclass PostRepository extends BaseRepository implements CacheableInterface {\\n\\n    use CacheableRepository;\\n\\n    ...\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nHere the repository will be cached , and the repository cache is cleared whenever an item is created, modified or deleted.\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Cache Config\"\n}\n[/block]\nYou can change the cache settings in the file config/repository.php and also directly on your repository.\n\n***config/repository.php***\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"'cache'=>[\\n    //Enable or disable cache repositories\\n    'enabled'   => true,\\n\\n    //Lifetime of cache\\n    'minutes'   => 30,\\n\\n    //Repository Cache, implementation Illuminate\\\\Contracts\\\\Cache\\\\Repository\\n    'repository'=> 'cache',\\n\\n    //Sets clearing the cache\\n    'clean'     => [\\n        //Enable, disable clearing the cache on changes\\n        'enabled' => true,\\n\\n        'on' => [\\n            //Enable, disable clearing the cache when you create an item\\n            'create'=>true,\\n\\n            //Enable, disable clearing the cache when upgrading an item\\n            'update'=>true,\\n\\n            //Enable, disable clearing the cache when you delete an item\\n            'delete'=>true,\\n        ]\\n    ],\\n    'params' => [\\n        //Request parameter that will be used to bypass the cache repository\\n        'skipCache'=>'skipCache'\\n    ],\\n    'allowed'=>[\\n        //Allow caching only for some methods\\n        'only'  =>null,\\n\\n        //Allow caching for all available methods, except\\n        'except'=>null\\n    ],\\n],\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nFurther, it is possible to override these settings directly in the repository.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"use Litepie\\\\Repository\\\\Eloquent\\\\BaseRepository;\\nuse Litepie\\\\Repository\\\\Contracts\\\\CacheableInterface;\\nuse Litepie\\\\Repository\\\\Traits\\\\CacheableRepository;\\n\\nclass PostRepository extends BaseRepository implements CacheableInterface {\\n\\n    // Setting the lifetime of the cache to a repository specifically\\n    protected $cacheMinutes = 90;\\n\\n    protected $cacheOnly = ['all', ...];\\n    //or\\n    protected $cacheExcept = ['find', ...];\\n\\n    use CacheableRepository;\\n\\n    ...\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nThe cacheable methods are : all, paginate, find, findByField, findWhere, getByCriteria\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"VALIDATORS\"\n}\n[/block]\nRequires **prettus/laravel-validator**. \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"composer require prettus/laravel-validator\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"USING A VALIDATOR CLASS\"\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Create a Validator\"\n}\n[/block]\nIn the example below, we define some rules for both creation and edition.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"use \\\\Prettus\\\\Validator\\\\LaravelValidator;\\n\\nclass PostValidator extends LaravelValidator {\\n\\n    protected $rules = [\\n        'title' => 'required',\\n        'text'  => 'min:3',\\n        'author'=> 'required'\\n    ];\\n\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nTo define specific rules, proceed as shown below:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"use \\\\Prettus\\\\Validator\\\\Contracts\\\\ValidatorInterface;\\nuse \\\\Prettus\\\\Validator\\\\LaravelValidator;\\n\\nclass PostValidator extends LaravelValidator {\\n\\n    protected $rules = [\\n        ValidatorInterface::RULE_CREATE => [\\n            'title' => 'required',\\n            'text'  => 'min:3',\\n            'author'=> 'required'\\n        ],\\n        ValidatorInterface::RULE_UPDATE => [\\n            'title' => 'required'\\n        ]\\n   ];\\n\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Enabling Validator in the Repository\"\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"use Litepie\\\\Repository\\\\Eloquent\\\\BaseRepository;\\nuse Litepie\\\\Repository\\\\Criteria\\\\RequestCriteria;\\n\\nclass PostRepository extends BaseRepository {\\n\\n    /**\\n     * Specify Model class name\\n     *\\n     * @return mixed\\n     */\\n    function model(){\\n       return \\\"App\\\\\\\\Post\\\";\\n    }\\n\\n    /**\\n     * Specify Validator class name\\n     *\\n     * @return mixed\\n     */\\n    public function validator()\\n    {\\n        return \\\"App\\\\\\\\PostValidator\\\";\\n    }\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Defining rules in the repository\"\n}\n[/block]\nAlternatively, instead of using a class to define its validation rules, the user can set your rules directly into the rules repository property, it will have the same effect as a Validation class.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"use Litepie\\\\Repository\\\\Eloquent\\\\BaseRepository;\\nuse Litepie\\\\Repository\\\\Criteria\\\\RequestCriteria;\\nuse Prettus\\\\Validator\\\\Contracts\\\\ValidatorInterface;\\n\\nclass PostRepository extends BaseRepository {\\n\\n    /**\\n     * Specify Validator Rules\\n     * @var array\\n     */\\n     protected $rules = [\\n        ValidatorInterface::RULE_CREATE => [\\n            'title' => 'required',\\n            'text'  => 'min:3',\\n            'author'=> 'required'\\n        ],\\n        ValidatorInterface::RULE_UPDATE => [\\n            'title' => 'required'\\n        ]\\n   ];\\n\\n    /**\\n     * Specify Model class name\\n     *\\n     * @return mixed\\n     */\\n    function model(){\\n       return \\\"App\\\\\\\\Post\\\";\\n    }\\n\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nValidation is now ready. In case of a failure an exception will be given of the type: ***Prettus\\Validator\\Exceptions\\ValidatorException***\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Presenters\"\n}\n[/block]\nPresenters function as a wrapper within the system and also a renderer for objects\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Fractal Presenter\"\n}\n[/block]\nThis requires [Fractal](http://fractal.thephpleague.com/) \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"composer require league/fractal\",\n      \"language\": \"text\"\n    }\n  ]\n}\n[/block]\nThere are two ways to implement the Presenter, \n\n1. The first is creating a TransformerAbstract and setting it using your Presenter class as described in the Create a Transformer Class.\n\n2. The second way is to make your model implement the Transformable interface, and use the default Presenter ModelFractarPresenter, this will have the same effect. \n\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Transformer Class\"\n}\n[/block]\nYou can **Create a transformer class **using the following command.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"php artisan make:transformer Post\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nThe code would generate the following class\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"use League\\\\Fractal\\\\TransformerAbstract;\\n\\nclass PostTransformer extends TransformerAbstract\\n{\\n    public function transform(\\\\Post $post)\\n    {\\n        return [\\n            'id'      => (int) $post->id,\\n            'title'   => $post->title,\\n            'content' => $post->content\\n        ];\\n    }\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Create a Presenter\"\n}\n[/block]\nCreate a Presenter using the command\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"php artisan make:presenter Post\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nThe command will further prompt you for creating a Transformer too if you haven't already.\n\n\nCreate a Presenter\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"use Litepie\\\\Repository\\\\Presenter\\\\FractalPresenter;\\n\\nclass PostPresenter extends FractalPresenter {\\n\\n    /**\\n     * Prepare data to present\\n     *\\n     * @return \\\\League\\\\Fractal\\\\TransformerAbstract\\n     */\\n    public function getTransformer()\\n    {\\n        return new PostTransformer();\\n    }\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nEnabling in your Repository\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"use Litepie\\\\Repository\\\\Eloquent\\\\BaseRepository;\\n\\nclass PostRepository extends BaseRepository {\\n\\n    ...\\n\\n    public function presenter()\\n    {\\n        return \\\"App\\\\\\\\Presenter\\\\\\\\PostPresenter\\\";\\n    }\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nOr enable it in your controller with the following code\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"$this->repository->setPresenter(\\\"App\\\\\\\\Presenter\\\\\\\\PostPresenter\\\");\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n**Using the presenter after from the Model** \n\nIf you recorded a presenter and sometime used the **skipPresenter()** method or simply you do not want your result is not changed automatically by the presenter. You can implement Presentable interface on your model so you will be able to present your model at any time. \n\nIn your model, implement the interface\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"Litepie\\\\Repository\\\\Contracts\\\\Presentable\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nAnd\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"Litepie\\\\Repository\\\\Traits\\\\PresentableTrait\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"namespace App;\\n\\nuse Litepie\\\\Repository\\\\Contracts\\\\Presentable;\\nuse Litepie\\\\Repository\\\\Traits\\\\PresentableTrait;\\n\\nclass Post extends Eloquent implements Presentable {\\n\\n    use PresentableTrait;\\n\\n    protected $fillable = [\\n        'title',\\n        'author',\\n        ...\\n     ];\\n\\n     ...\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nOnce thsi is done, the user can submit your Model individually, See an example:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"$repository = app('App\\\\PostRepository');\\n$repository->setPresenter(\\\"Litepie\\\\\\\\Repository\\\\\\\\Presenter\\\\\\\\ModelFractalPresenter\\\");\\n\\n//Getting the result transformed by the presenter directly in the search\\n$post = $repository->find(1);\\n\\nprint_r( $post ); //It produces an output as array\\n\\n...\\n\\n//Skip presenter and bringing the original result of the Model\\n$post = $repository->skipPresenter()->find(1);\\n\\nprint_r( $post ); //It produces an output as a Model object\\nprint_r( $post->presenter() ); //It produces an output as array\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nYou can skip the presenter at every visit and use it on demand directly into the model, for it set the ***$skipPresenter*** attribute to true in your repository:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"use Litepie\\\\Repository\\\\Eloquent\\\\BaseRepository;\\n\\nclass PostRepository extends BaseRepository {\\n\\n    /**\\n    * @var bool\\n    */\\n    protected $skipPresenter = true;\\n\\n    public function presenter()\\n    {\\n        return \\\"App\\\\\\\\Presenter\\\\\\\\PostPresenter\\\";\\n    }\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Model Class\"\n}\n[/block]\n**Implement Interface**\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"namespace App;\\n\\nuse Litepie\\\\Repository\\\\Contracts\\\\Transformable;\\n\\nclass Post extends Eloquent implements Transformable {\\n     ...\\n     /**\\n      * @return array\\n      */\\n     public function transform()\\n     {\\n         return [\\n             'id'      => (int) $this->id,\\n             'title'   => $this->title,\\n             'content' => $this->content\\n         ];\\n     }\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n**Enabling in your Repository**\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"Litepie\\\\Repository\\\\Presenter\\\\ModelFractalPresenter\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nThis Presenter is available for all Models which iplements Transformable\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"use Litepie\\\\Repository\\\\Eloquent\\\\BaseRepository;\\n\\nclass PostRepository extends BaseRepository {\\n\\n    ...\\n\\n    public function presenter()\\n    {\\n        return \\\"Litepie\\\\\\\\Repository\\\\\\\\Presenter\\\\\\\\ModelFractalPresenter\\\";\\n    }\\n}\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nYou can also enable it in your controller with\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"$this->repository->setPresenter(\\\"Litepie\\\\\\\\Repository\\\\\\\\Presenter\\\\\\\\ModelFractalPresenter\\\");\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\n\n[block:api-header]\n{\n  \"type\": \"basic\",\n  \"title\": \"Skip Presenter defined in the repository\"\n}\n[/block]\nUse ***skipPresenter ***before any other chaining method\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"$posts = $this->repository->skipPresenter()->all();\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]\nOr\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"$this->repository->skipPresenter();\\n\\n$posts = $this->repository->all();\",\n      \"language\": \"php\"\n    }\n  ]\n}\n[/block]","excerpt":"","slug":"database-1","type":"basic","title":"Repository"}
Litepie Repository is used to abstract the data layer. This allows the user to create a more flexible application and further providing an ease of maintenance on the same. [block:api-header] { "type": "basic", "title": "Repository Interface" } [/block] The Link to the same can be found under [block:code] { "codes": [ { "code": "Litepie\\Repository\\Contracts\\RepositoryInterface", "language": "php" } ] } [/block] The following methods are present under the Repository Interface [block:code] { "codes": [ { "code": "all($columns = array('*'))\nfirst($columns = array('*'))\npaginate($limit = null, $columns = ['*'])\nfind($id, $columns = ['*'])\nfindByField($field, $value, $columns = ['*'])\nfindWhere(array $where, $columns = ['*'])\nfindWhereIn($field, array $where, $columns = [*])\nfindWhereNotIn($field, array $where, $columns = [*])\ncreate(array $attributes)\nupdate(array $attributes, $id)\ndelete($id)\nwith(array $relations);\nhidden(array $fields);\nvisible(array $fields);\nscopeQuery(Closure $scope);\ngetFieldsSearchable();\nsetPresenter($presenter);\nskipPresenter($status = true);", "language": "php" } ] } [/block] [block:api-header] { "type": "basic", "title": "Repository Criteria Interface" } [/block] The Link to the same can be found under [block:code] { "codes": [ { "code": "Litepie\\Repository\\Contracts\\RepositoryCriteriaInterface", "language": "php" } ] } [/block] The following methods are present under the Repository Criteria Interface [block:code] { "codes": [ { "code": "pushCriteria(CriteriaInterface $criteria)\ngetCriteria()\ngetByCriteria(CriteriaInterface $criteria)\nskipCriteria($status = true)\ngetFieldsSearchable()", "language": "php" } ] } [/block] [block:api-header] { "type": "basic", "title": "Cacheable Interface" } [/block] The Link to the same can be found under [block:code] { "codes": [ { "code": "Litepie\\Repository\\Contracts\\CacheableInterface", "language": "php" } ] } [/block] The following methods are present under the Cacheable Interface [block:code] { "codes": [ { "code": "setCacheRepository(CacheRepository $repository)\ngetCacheRepository()\ngetCacheKey($method, $args = null)\ngetCacheMinutes()\nskipCache($status = true)", "language": "php" } ] } [/block] [block:api-header] { "type": "basic", "title": "Presenter Interface" } [/block] The Link to the same can be found under [block:code] { "codes": [ { "code": "Litepie\\Repository\\Contracts\\PresenterInterface", "language": "php", "name": null } ] } [/block] The following methods are present under the Cacheable Interface [block:code] { "codes": [ { "code": "present($data);", "language": "php", "name": null } ] } [/block] [block:api-header] { "type": "basic", "title": "Presentable" } [/block] The Link to the same can be found under [block:code] { "codes": [ { "code": "Litepie\\Repository\\Contracts\\Presentable", "language": "php" } ] } [/block] The following methods are present under Presentable [block:code] { "codes": [ { "code": "setPresenter(PresenterInterface $presenter);\npresenter();", "language": "php" } ] } [/block] [block:api-header] { "type": "basic", "title": "Criteria Interface" } [/block] The Link to the same can be found under [block:code] { "codes": [ { "code": "Litepie\\Repository\\Contracts\\CriteriaInterface", "language": "php", "name": null } ] } [/block] The following methods under Criteria interface are as follows [block:code] { "codes": [ { "code": "apply($model, RepositoryInterface $repository);", "language": "php" } ] } [/block] [block:api-header] { "type": "basic", "title": "Transformable" } [/block] The Link to the same can be found under [block:code] { "codes": [ { "code": "Litepie\\Repository\\Contracts\\Transformable", "language": "php" } ] } [/block] The following methods under Tranformable are as follows [block:code] { "codes": [ { "code": "transform();", "language": "php" } ] } [/block] [block:api-header] { "type": "basic", "title": "USAGE" } [/block] [block:api-header] { "type": "basic", "title": "CREATE A MODEL" } [/block] The user can use the code to create a model normally, however it is important to ensure that attributes are defined from the input data. [block:code] { "codes": [ { "code": "namespace App;\n\nclass Post extends Eloquent { // or Ardent, Or any other Model Class\n\n protected $fillable = [\n 'title',\n 'author',\n ...\n ];\n\n ...\n}", "language": "php" } ] } [/block] [block:api-header] { "type": "basic", "title": "CREATE A REPOSITORY" } [/block] The user can develop a Repository for your Database by extending the Base repository. [block:code] { "codes": [ { "code": "namespace App;\n\nuse Litepie\\Repository\\Eloquent\\BaseRepository;\n\nclass PostRepository extends BaseRepository {\n\n /**\n * Specify Model class name\n *\n * @return string\n */\n function model()\n {\n return \"App\\\\Post\";\n }\n}", "language": "php" } ] } [/block] [block:api-header] { "type": "basic", "title": "GENERATORS" } [/block] The Generators have enable users to create Repositories with ease. [block:api-header] { "type": "basic", "title": "Config" } [/block] The user must first configure the respective storage location of the repository files. This is by default done through the "app" folder and the namespace "App". [block:code] { "codes": [ { "code": "...\n 'generator'=>[\n 'basePath'=>app_path(),\n 'rootNamespace'=>'App\\\\',\n 'paths'=>[\n 'models'=>'Entities',\n 'repositories'=>'Repositories',\n 'interfaces'=>'Repositories',\n 'transformers'=>'Transformers',\n 'presenters'=>'Presenters'\n ]\n ]", "language": "php" } ] } [/block] The user may want to save the root of your project folder outside the location of the app and add another namespace, For Eg. [block:code] { "codes": [ { "code": "...\n 'generator'=>[\n 'basePath' => base_path('src/Lorem'),\n 'rootNamespace' => 'Lorem\\\\'\n ]", "language": "php" } ] } [/block] Additionally, the user may wish to customize where your generated classes gets saved. This can be accomplished by editing the **paths **node to your liking. For Eg. [block:code] { "codes": [ { "code": " 'generator'=>[\n 'basePath'=>app_path(),\n 'rootNamespace'=>'App\\\\',\n 'paths'=>[\n 'models'=>'Models',\n 'repositories'=>'Repositories\\\\Eloquent',\n 'interfaces'=>'Contracts\\\\Repositories',\n 'transformers'=>'Transformers',\n 'presenters'=>'Presenters'\n ]\n ]", "language": "php" } ] } [/block] [block:api-header] { "type": "basic", "title": "COMMANDS" } [/block] In order to generate all the functions that the user needs for the respective Model, this command can be run: [block:code] { "codes": [ { "code": "php artisan make:entity Post", "language": "php" } ] } [/block] This will create the Model, the Repository, the Presenter and the Transformer classes. The user can also pass the options from the **repository **command, since this command is just a wrapper. To generate a repository for your Post model, the following command can be utilized [block:code] { "codes": [ { "code": "php artisan make:repository Post", "language": "php" } ] } [/block] If you would like to implement a namespace to your blog, you could utilize the following command. [block:code] { "codes": [ { "code": "php artisan make:repository \"Blog\\Post\"", "language": "php" } ] } [/block] There are certain Added fields that are fillable by the user. [block:code] { "codes": [ { "code": "php artisan make:repository \"Blog\\Post\" --fillable=\"title,content\"", "language": "php" } ] } [/block] When the user is running commado, he/she will be creating the "Entities" folder and "Repositories" inside the folder that you set as the default. Done, done that just now you do bind its interface for your real repository, for example in your own Repositories Service Provider. [block:code] { "codes": [ { "code": "App::bind('{YOUR_NAMESPACE}Repositories\\PostRepository', '{YOUR_NAMESPACE}Repositories\\PostRepositoryEloquent');", "language": "php" } ] } [/block] And use [block:code] { "codes": [ { "code": "public function __construct({YOUR_NAMESPACE}Repositories\\PostRepository $repository){\n $this->repository = $repository;\n}", "language": "php" } ] } [/block] [block:api-header] { "type": "basic", "title": "USE METHOD" } [/block] [block:code] { "codes": [ { "code": "namespace App\\Http\\Controllers;\n\nuse App\\PostRepository;\n\nclass PostsController extends BaseController {\n\n /**\n * @var PostRepository\n */\n protected $repository;\n\n public function __construct(PostRepository $repository){\n $this->repository = $repository;\n }\n\n ....\n}", "language": "php" } ] } [/block] Find all results in Repository [block:code] { "codes": [ { "code": "$posts = $this->repository->all();", "language": "php" } ] } [/block] Find all results in Repository with pagination [block:code] { "codes": [ { "code": "$posts = $this->repository->paginate($limit = null, $columns = ['*']);", "language": "php" } ] } [/block] Find by result by id [block:code] { "codes": [ { "code": "$post = $this->repository->find($id);", "language": "php" } ] } [/block] Hiding attributes of the model [block:code] { "codes": [ { "code": "$post = $this->repository->hidden(['country_id'])->find($id);", "language": "php" } ] } [/block] Showing only specific attributes of the model [block:code] { "codes": [ { "code": "$post = $this->repository->visible(['id', 'state_id'])->find($id);", "language": "php" } ] } [/block] Loading the Model relationships [block:code] { "codes": [ { "code": "$post = $this->repository->with(['state'])->find($id);", "language": "php" } ] } [/block] Find by result by field name [block:code] { "codes": [ { "code": "$posts = $this->repository->findByField('country_id','15');", "language": "php" } ] } [/block] Find by result by multiple fields [block:code] { "codes": [ { "code": "$posts = $this->repository->findWhere([\n //Default Condition =\n 'state_id'=>'10',\n 'country_id'=>'15',\n //Custom Condition\n ['columnName','>','10']\n]);", "language": "php" } ] } [/block] Find by result by multiple values in one field [block:code] { "codes": [ { "code": "$posts = $this->repository->findWhereIn('id', [1,2,3,4,5]);", "language": "php" } ] } [/block] Find by result by excluding multiple values in one field [block:code] { "codes": [ { "code": "$posts = $this->repository->findWhereNotIn('id', [6,7,8,9,10]);", "language": "php" } ] } [/block] Find all using custom scope [block:code] { "codes": [ { "code": "$posts = $this->repository->scopeQuery(function($query){\n return $query->orderBy('sort_order','asc');\n})->all();", "language": "php" } ] } [/block] Create new entry in Repository [block:code] { "codes": [ { "code": "$post = $this->repository->create( Input::all() );", "language": "php" } ] } [/block] Update entry in Repository [block:code] { "codes": [ { "code": "$post = $this->repository->update( Input::all(), $id );", "language": "php" } ] } [/block] Delete entry in Repository [block:code] { "codes": [ { "code": "$this->repository->delete($id)", "language": "php" } ] } [/block] [block:api-header] { "type": "basic", "title": "CREATE A CRITERIA" } [/block] The Criteria is a way to change the repository of the query by applying specific conditions according to the user's needs. The user can add multiple Criteria into the repository. [block:code] { "codes": [ { "code": "use Litepie\\Repository\\Contracts\\RepositoryInterface;\nuse Litepie\\Repository\\Contracts\\CriteriaInterface;\n\nclass MyCriteria implements CriteriaInterface {\n\n public function apply($model, RepositoryInterface $repository)\n {\n $model = $model->where('user_id','=', Auth::user()->id );\n return $model;\n }\n}", "language": "php" } ] } [/block] [block:api-header] { "type": "basic", "title": "USING THE CRITERIA IN A CONTROLLER" } [/block] The criteria can be utilized in the controller using the following code. [block:code] { "codes": [ { "code": "namespace App\\Http\\Controllers;\n\nuse App\\PostRepository;\n\nclass PostsController extends BaseController {\n\n /**\n * @var PostRepository\n */\n protected $repository;\n\n public function __construct(PostRepository $repository){\n $this->repository = $repository;\n }\n\n\n public function index()\n {\n $this->repository->pushCriteria(new MyCriteria());\n $posts = $this->repository->all();\n ...\n }\n\n}", "language": "php" } ] } [/block] The user could obtain the results from Criteria utilizing the following code. [block:code] { "codes": [ { "code": "$posts = $this->repository->getByCriteria(new MyCriteria());", "language": "php" } ] } [/block] The user can set the default Criteria in Repository [block:code] { "codes": [ { "code": "use Litepie\\Repository\\Eloquent\\BaseRepository;\n\nclass PostRepository extends BaseRepository {\n\n public function boot(){\n $this->pushCriteria(new MyCriteria());\n $this->pushCriteria(new AnotherCriteria());\n ...\n }\n\n function model(){\n return \"App\\\\Post\";\n }\n}", "language": "php" } ] } [/block] [block:api-header] { "type": "basic", "title": "SKIP CRITERIA" } [/block] The user can utilize the **skipCriteria** before any chaining method. [block:code] { "codes": [ { "code": "$posts = $this->repository->skipCriteria()->all();", "language": "php" } ] } [/block] [block:api-header] { "type": "basic", "title": "USING THE REQUESTCRITERIA" } [/block] The RequestCriteria is a standard Criteria implementation. It enables filters to perform in the repository from parameters sent in the request. The user can perform a dynamic search, filter the data and customize the queries. To use the Criteria in your repository, the user can add a new criteria in the boot method of your repository, or directly use in your controller, in order to filter out only a few requests. [block:api-header] { "type": "basic", "title": "ENABLING YOUR REPOSITORY" } [/block] [block:code] { "codes": [ { "code": "use Litepie\\Repository\\Eloquent\\BaseRepository;\nuse Litepie\\Repository\\Criteria\\RequestCriteria;\n\n\nclass PostRepository extends BaseRepository {\n\n /**\n * @var array\n */\n protected $fieldSearchable = [\n 'name',\n 'email'\n ];\n\n public function boot(){\n $this->pushCriteria(app('Litepie\\Repository\\Criteria\\RequestCriteria'));\n ...\n }\n\n function model(){\n return \"App\\\\Post\";\n }\n}", "language": "php" } ] } [/block] The user would need to define which fields from the model can be searchable. In the repository the fields that are searchable should be set to $fieldSearchable. [block:code] { "codes": [ { "code": "protected $fieldSearchable = [\n 'name',\n 'email'\n];", "language": "php" } ] } [/block] You can set the type of condition which will be used to perform the query, the default condition is **"="** [block:code] { "codes": [ { "code": "protected $fieldSearchable = [\n 'name'=>'like',\n 'email', // Default Condition \"=\"\n 'your_field'=>'condition'\n];", "language": "php" } ] } [/block] [block:api-header] { "type": "basic", "title": "ENABLING IN CONTROLLER" } [/block] [block:code] { "codes": [ { "code": " public function index()\n {\n $this->repository->pushCriteria(app('Litepie\\Repository\\Criteria\\RequestCriteria'));\n $posts = $this->repository->all();\n ...\n }", "language": "php" } ] } [/block] [block:api-header] { "type": "basic", "title": "EXAMPLE THE CRITERIA" } [/block] The user can Request all data without filter by request [block:code] { "codes": [ { "code": "http://prettus.local/users", "language": "text" } ] } [/block] [block:code] { "codes": [ { "code": "[\n {\n \"id\": 1,\n \"name\": \"John Doe\",\n \"email\": \"john@gmail.com\",\n \"created_at\": \"-0001-11-30 00:00:00\",\n \"updated_at\": \"-0001-11-30 00:00:00\"\n },\n {\n \"id\": 2,\n \"name\": \"Lorem Ipsum\",\n \"email\": \"lorem@ipsum.com\",\n \"created_at\": \"-0001-11-30 00:00:00\",\n \"updated_at\": \"-0001-11-30 00:00:00\"\n },\n {\n \"id\": 3,\n \"name\": \"Laravel\",\n \"email\": \"laravel@gmail.com\",\n \"created_at\": \"-0001-11-30 00:00:00\",\n \"updated_at\": \"-0001-11-30 00:00:00\"\n }\n]", "language": "php" } ] } [/block] Conducting research in the repository [block:code] { "codes": [ { "code": "http://prettus.local/users?search=John%20Doe", "language": "php" } ] } [/block] ***Or*** [block:code] { "codes": [ { "code": "http://prettus.local/users?search=John&searchFields=name:like", "language": "php" } ] } [/block] ***Or*** [block:code] { "codes": [ { "code": "http://prettus.local/users?search=john@gmail.com&searchFields=email:=", "language": "php" } ] } [/block] ***Or*** [block:code] { "codes": [ { "code": "http://prettus.local/users?search=name:John Doe;email:john@gmail.com", "language": "php" } ] } [/block] ***Or*** [block:code] { "codes": [ { "code": "http://prettus.local/users", "language": "text" } ] } [/block] [block:code] { "codes": [ { "code": "?search=name:John;email:john@gmail.com&searchFields=name:like;email:=", "language": "php" } ] } [/block] [block:code] { "codes": [ { "code": "[\n {\n \"id\": 1,\n \"name\": \"John Doe\",\n \"email\": \"john@gmail.com\",\n \"created_at\": \"-0001-11-30 00:00:00\",\n \"updated_at\": \"-0001-11-30 00:00:00\"\n }\n]", "language": "php" } ] } [/block] Filtering fields [block:code] { "codes": [ { "code": "http://prettus.local/users?filter=id;name", "language": "text" } ] } [/block] [block:code] { "codes": [ { "code": "[\n {\n \"id\": 1,\n \"name\": \"John Doe\"\n },\n {\n \"id\": 2,\n \"name\": \"Lorem Ipsum\"\n },\n {\n \"id\": 3,\n \"name\": \"Laravel\"\n }\n]", "language": "php" } ] } [/block] Sorting the results [block:code] { "codes": [ { "code": "http://prettus.local/users?filter=id;name&orderBy=id&sortedBy=desc", "language": "text" } ] } [/block] [block:code] { "codes": [ { "code": "[\n {\n \"id\": 3,\n \"name\": \"Laravel\"\n },\n {\n \"id\": 2,\n \"name\": \"Lorem Ipsum\"\n },\n {\n \"id\": 1,\n \"name\": \"John Doe\"\n }\n]", "language": "php" } ] } [/block] Add relationship [block:code] { "codes": [ { "code": "http://prettus.local/users?with=groups", "language": "text" } ] } [/block] [block:api-header] { "type": "basic", "title": "OVERWRITE PARAMS NAME" } [/block] The user can change the name of the parameters in the configuration file **config/repository.php** [block:api-header] { "type": "basic", "title": "CACHE" } [/block] The user can further add a layer of cache easily to your repository [block:api-header] { "type": "basic", "title": "Cache Usage" } [/block] Implements the interface CacheableInterface and use CacheableRepository Trait. [block:code] { "codes": [ { "code": "use Litepie\\Repository\\Eloquent\\BaseRepository;\nuse Litepie\\Repository\\Contracts\\CacheableInterface;\nuse Litepie\\Repository\\Traits\\CacheableRepository;\n\nclass PostRepository extends BaseRepository implements CacheableInterface {\n\n use CacheableRepository;\n\n ...\n}", "language": "php" } ] } [/block] Here the repository will be cached , and the repository cache is cleared whenever an item is created, modified or deleted. [block:api-header] { "type": "basic", "title": "Cache Config" } [/block] You can change the cache settings in the file config/repository.php and also directly on your repository. ***config/repository.php*** [block:code] { "codes": [ { "code": "'cache'=>[\n //Enable or disable cache repositories\n 'enabled' => true,\n\n //Lifetime of cache\n 'minutes' => 30,\n\n //Repository Cache, implementation Illuminate\\Contracts\\Cache\\Repository\n 'repository'=> 'cache',\n\n //Sets clearing the cache\n 'clean' => [\n //Enable, disable clearing the cache on changes\n 'enabled' => true,\n\n 'on' => [\n //Enable, disable clearing the cache when you create an item\n 'create'=>true,\n\n //Enable, disable clearing the cache when upgrading an item\n 'update'=>true,\n\n //Enable, disable clearing the cache when you delete an item\n 'delete'=>true,\n ]\n ],\n 'params' => [\n //Request parameter that will be used to bypass the cache repository\n 'skipCache'=>'skipCache'\n ],\n 'allowed'=>[\n //Allow caching only for some methods\n 'only' =>null,\n\n //Allow caching for all available methods, except\n 'except'=>null\n ],\n],", "language": "php" } ] } [/block] Further, it is possible to override these settings directly in the repository. [block:code] { "codes": [ { "code": "use Litepie\\Repository\\Eloquent\\BaseRepository;\nuse Litepie\\Repository\\Contracts\\CacheableInterface;\nuse Litepie\\Repository\\Traits\\CacheableRepository;\n\nclass PostRepository extends BaseRepository implements CacheableInterface {\n\n // Setting the lifetime of the cache to a repository specifically\n protected $cacheMinutes = 90;\n\n protected $cacheOnly = ['all', ...];\n //or\n protected $cacheExcept = ['find', ...];\n\n use CacheableRepository;\n\n ...\n}", "language": "php" } ] } [/block] The cacheable methods are : all, paginate, find, findByField, findWhere, getByCriteria [block:api-header] { "type": "basic", "title": "VALIDATORS" } [/block] Requires **prettus/laravel-validator**. [block:code] { "codes": [ { "code": "composer require prettus/laravel-validator", "language": "text" } ] } [/block] [block:api-header] { "type": "basic", "title": "USING A VALIDATOR CLASS" } [/block] [block:api-header] { "type": "basic", "title": "Create a Validator" } [/block] In the example below, we define some rules for both creation and edition. [block:code] { "codes": [ { "code": "use \\Prettus\\Validator\\LaravelValidator;\n\nclass PostValidator extends LaravelValidator {\n\n protected $rules = [\n 'title' => 'required',\n 'text' => 'min:3',\n 'author'=> 'required'\n ];\n\n}", "language": "php" } ] } [/block] To define specific rules, proceed as shown below: [block:code] { "codes": [ { "code": "use \\Prettus\\Validator\\Contracts\\ValidatorInterface;\nuse \\Prettus\\Validator\\LaravelValidator;\n\nclass PostValidator extends LaravelValidator {\n\n protected $rules = [\n ValidatorInterface::RULE_CREATE => [\n 'title' => 'required',\n 'text' => 'min:3',\n 'author'=> 'required'\n ],\n ValidatorInterface::RULE_UPDATE => [\n 'title' => 'required'\n ]\n ];\n\n}", "language": "php" } ] } [/block] [block:api-header] { "type": "basic", "title": "Enabling Validator in the Repository" } [/block] [block:code] { "codes": [ { "code": "use Litepie\\Repository\\Eloquent\\BaseRepository;\nuse Litepie\\Repository\\Criteria\\RequestCriteria;\n\nclass PostRepository extends BaseRepository {\n\n /**\n * Specify Model class name\n *\n * @return mixed\n */\n function model(){\n return \"App\\\\Post\";\n }\n\n /**\n * Specify Validator class name\n *\n * @return mixed\n */\n public function validator()\n {\n return \"App\\\\PostValidator\";\n }\n}", "language": "php" } ] } [/block] [block:api-header] { "type": "basic", "title": "Defining rules in the repository" } [/block] Alternatively, instead of using a class to define its validation rules, the user can set your rules directly into the rules repository property, it will have the same effect as a Validation class. [block:code] { "codes": [ { "code": "use Litepie\\Repository\\Eloquent\\BaseRepository;\nuse Litepie\\Repository\\Criteria\\RequestCriteria;\nuse Prettus\\Validator\\Contracts\\ValidatorInterface;\n\nclass PostRepository extends BaseRepository {\n\n /**\n * Specify Validator Rules\n * @var array\n */\n protected $rules = [\n ValidatorInterface::RULE_CREATE => [\n 'title' => 'required',\n 'text' => 'min:3',\n 'author'=> 'required'\n ],\n ValidatorInterface::RULE_UPDATE => [\n 'title' => 'required'\n ]\n ];\n\n /**\n * Specify Model class name\n *\n * @return mixed\n */\n function model(){\n return \"App\\\\Post\";\n }\n\n}", "language": "php" } ] } [/block] Validation is now ready. In case of a failure an exception will be given of the type: ***Prettus\Validator\Exceptions\ValidatorException*** [block:api-header] { "type": "basic", "title": "Presenters" } [/block] Presenters function as a wrapper within the system and also a renderer for objects [block:api-header] { "type": "basic", "title": "Fractal Presenter" } [/block] This requires [Fractal](http://fractal.thephpleague.com/) [block:code] { "codes": [ { "code": "composer require league/fractal", "language": "text" } ] } [/block] There are two ways to implement the Presenter, 1. The first is creating a TransformerAbstract and setting it using your Presenter class as described in the Create a Transformer Class. 2. The second way is to make your model implement the Transformable interface, and use the default Presenter ModelFractarPresenter, this will have the same effect. [block:api-header] { "type": "basic", "title": "Transformer Class" } [/block] You can **Create a transformer class **using the following command. [block:code] { "codes": [ { "code": "php artisan make:transformer Post", "language": "php" } ] } [/block] The code would generate the following class [block:code] { "codes": [ { "code": "use League\\Fractal\\TransformerAbstract;\n\nclass PostTransformer extends TransformerAbstract\n{\n public function transform(\\Post $post)\n {\n return [\n 'id' => (int) $post->id,\n 'title' => $post->title,\n 'content' => $post->content\n ];\n }\n}", "language": "php" } ] } [/block] [block:api-header] { "type": "basic", "title": "Create a Presenter" } [/block] Create a Presenter using the command [block:code] { "codes": [ { "code": "php artisan make:presenter Post", "language": "php" } ] } [/block] The command will further prompt you for creating a Transformer too if you haven't already. Create a Presenter [block:code] { "codes": [ { "code": "use Litepie\\Repository\\Presenter\\FractalPresenter;\n\nclass PostPresenter extends FractalPresenter {\n\n /**\n * Prepare data to present\n *\n * @return \\League\\Fractal\\TransformerAbstract\n */\n public function getTransformer()\n {\n return new PostTransformer();\n }\n}", "language": "php" } ] } [/block] Enabling in your Repository [block:code] { "codes": [ { "code": "use Litepie\\Repository\\Eloquent\\BaseRepository;\n\nclass PostRepository extends BaseRepository {\n\n ...\n\n public function presenter()\n {\n return \"App\\\\Presenter\\\\PostPresenter\";\n }\n}", "language": "php" } ] } [/block] Or enable it in your controller with the following code [block:code] { "codes": [ { "code": "$this->repository->setPresenter(\"App\\\\Presenter\\\\PostPresenter\");", "language": "php" } ] } [/block] **Using the presenter after from the Model** If you recorded a presenter and sometime used the **skipPresenter()** method or simply you do not want your result is not changed automatically by the presenter. You can implement Presentable interface on your model so you will be able to present your model at any time. In your model, implement the interface [block:code] { "codes": [ { "code": "Litepie\\Repository\\Contracts\\Presentable", "language": "php" } ] } [/block] And [block:code] { "codes": [ { "code": "Litepie\\Repository\\Traits\\PresentableTrait", "language": "php" } ] } [/block] [block:code] { "codes": [ { "code": "namespace App;\n\nuse Litepie\\Repository\\Contracts\\Presentable;\nuse Litepie\\Repository\\Traits\\PresentableTrait;\n\nclass Post extends Eloquent implements Presentable {\n\n use PresentableTrait;\n\n protected $fillable = [\n 'title',\n 'author',\n ...\n ];\n\n ...\n}", "language": "php" } ] } [/block] Once thsi is done, the user can submit your Model individually, See an example: [block:code] { "codes": [ { "code": "$repository = app('App\\PostRepository');\n$repository->setPresenter(\"Litepie\\\\Repository\\\\Presenter\\\\ModelFractalPresenter\");\n\n//Getting the result transformed by the presenter directly in the search\n$post = $repository->find(1);\n\nprint_r( $post ); //It produces an output as array\n\n...\n\n//Skip presenter and bringing the original result of the Model\n$post = $repository->skipPresenter()->find(1);\n\nprint_r( $post ); //It produces an output as a Model object\nprint_r( $post->presenter() ); //It produces an output as array", "language": "php" } ] } [/block] You can skip the presenter at every visit and use it on demand directly into the model, for it set the ***$skipPresenter*** attribute to true in your repository: [block:code] { "codes": [ { "code": "use Litepie\\Repository\\Eloquent\\BaseRepository;\n\nclass PostRepository extends BaseRepository {\n\n /**\n * @var bool\n */\n protected $skipPresenter = true;\n\n public function presenter()\n {\n return \"App\\\\Presenter\\\\PostPresenter\";\n }\n}", "language": "php" } ] } [/block] [block:api-header] { "type": "basic", "title": "Model Class" } [/block] **Implement Interface** [block:code] { "codes": [ { "code": "namespace App;\n\nuse Litepie\\Repository\\Contracts\\Transformable;\n\nclass Post extends Eloquent implements Transformable {\n ...\n /**\n * @return array\n */\n public function transform()\n {\n return [\n 'id' => (int) $this->id,\n 'title' => $this->title,\n 'content' => $this->content\n ];\n }\n}", "language": "php" } ] } [/block] **Enabling in your Repository** [block:code] { "codes": [ { "code": "Litepie\\Repository\\Presenter\\ModelFractalPresenter", "language": "php" } ] } [/block] This Presenter is available for all Models which iplements Transformable [block:code] { "codes": [ { "code": "use Litepie\\Repository\\Eloquent\\BaseRepository;\n\nclass PostRepository extends BaseRepository {\n\n ...\n\n public function presenter()\n {\n return \"Litepie\\\\Repository\\\\Presenter\\\\ModelFractalPresenter\";\n }\n}", "language": "php" } ] } [/block] You can also enable it in your controller with [block:code] { "codes": [ { "code": "$this->repository->setPresenter(\"Litepie\\\\Repository\\\\Presenter\\\\ModelFractalPresenter\");", "language": "php" } ] } [/block] [block:api-header] { "type": "basic", "title": "Skip Presenter defined in the repository" } [/block] Use ***skipPresenter ***before any other chaining method [block:code] { "codes": [ { "code": "$posts = $this->repository->skipPresenter()->all();", "language": "php" } ] } [/block] Or [block:code] { "codes": [ { "code": "$this->repository->skipPresenter();\n\n$posts = $this->repository->all();", "language": "php" } ] } [/block]