{"id":21734810,"url":"https://github.com/bosnadev/repository","last_synced_at":"2025-03-12T09:06:07.590Z","repository":{"id":27503288,"uuid":"30983690","full_name":"bosnadev/repository","owner":"bosnadev","description":"Laravel Repositories is a package for Laravel 5 which is used to abstract the database layer. This makes applications much easier to maintain.","archived":false,"fork":false,"pushed_at":"2022-07-17T17:28:21.000Z","size":118,"stargazers_count":827,"open_issues_count":75,"forks_count":241,"subscribers_count":58,"default_branch":"master","last_synced_at":"2025-03-05T08:06:51.212Z","etag":null,"topics":["criteria","database-layer","laravel","laravel-repositories","php"],"latest_commit_sha":null,"homepage":"https://bosnadev.com","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/bosnadev.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-02-18T19:57:34.000Z","updated_at":"2025-02-22T03:56:31.000Z","dependencies_parsed_at":"2022-08-31T21:21:43.179Z","dependency_job_id":null,"html_url":"https://github.com/bosnadev/repository","commit_stats":null,"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bosnadev%2Frepository","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bosnadev%2Frepository/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bosnadev%2Frepository/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/bosnadev%2Frepository/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/bosnadev","download_url":"https://codeload.github.com/bosnadev/repository/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243185453,"owners_count":20250188,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["criteria","database-layer","laravel","laravel-repositories","php"],"created_at":"2024-11-26T05:10:42.189Z","updated_at":"2025-03-12T09:06:07.568Z","avatar_url":"https://github.com/bosnadev.png","language":"PHP","funding_links":[],"categories":["PHP"],"sub_categories":[],"readme":"# Laravel Repositories\n\n[![Build Status](https://travis-ci.org/bosnadev/repository.svg?branch=master)](https://travis-ci.org/bosnadev/repository) \n[![SensioLabsInsight](https://img.shields.io/sensiolabs/i/f39e6dc7-1364-481d-b722-8413bdc3200f.svg?style=flat)](https://insight.sensiolabs.com/projects/f39e6dc7-1364-481d-b722-8413bdc3200f)\n[![Latest Stable Version](https://poser.pugx.org/bosnadev/repositories/v/stable)](https://packagist.org/packages/bosnadev/repositories)\n[![Total Downloads](https://poser.pugx.org/bosnadev/repositories/downloads)](https://packagist.org/packages/bosnadev/repositories)\n[![Monthly Downloads](https://poser.pugx.org/bosnadev/repositories/d/monthly)](https://packagist.org/packages/bosnadev/repositories)\n[![License](https://poser.pugx.org/bosnadev/repositories/license)](https://packagist.org/packages/bosnadev/repositories)\n\nLaravel Repositories is a package for Laravel 5 which is used to abstract the database layer. This makes applications much easier to maintain.\n\n## Installation\n\nRun the following command from you terminal:\n\n\n ```bash\n composer require \"bosnadev/repositories: 0.*\"\n ```\n\nor add this to require section in your composer.json file:\n\n ```\n \"bosnadev/repositories\": \"0.*\"\n ```\n\nthen run ```composer update```\n\n\n## Usage\n\nFirst, create your repository class. Note that your repository class MUST extend ```Bosnadev\\Repositories\\Eloquent\\Repository``` and implement model() method\n\n```php\n\u003c?php namespace App\\Repositories;\n\nuse Bosnadev\\Repositories\\Contracts\\RepositoryInterface;\nuse Bosnadev\\Repositories\\Eloquent\\Repository;\n\nclass FilmsRepository extends Repository {\n\n    public function model() {\n        return 'App\\Film';\n    }\n}\n```\n\nBy implementing ```model()``` method you telling repository what model class you want to use. Now, create ```App\\Film``` model:\n\n```php\n\u003c?php namespace App;\n\nuse Illuminate\\Database\\Eloquent\\Model;\n\nclass Film extends Model {\n\n    protected $primaryKey = 'film_id';\n\n    protected $table = 'film';\n\n    protected $casts = [\n        \"rental_rate\"       =\u003e 'float'\n    ];\n}\n```\n\nAnd finally, use the repository in the controller:\n\n```php\n\u003c?php namespace App\\Http\\Controllers;\n\nuse App\\Repositories\\FilmsRepository as Film;\n\nclass FilmsController extends Controller {\n\n    private $film;\n\n    public function __construct(Film $film) {\n\n        $this-\u003efilm = $film;\n    }\n\n    public function index() {\n        return \\Response::json($this-\u003efilm-\u003eall());\n    }\n}\n```\n\n## Available Methods\n\nThe following methods are available:\n\n##### Bosnadev\\Repositories\\Contracts\\RepositoryInterface\n\n```php\npublic function all($columns = array('*'))\npublic function lists($value, $key = null)\npublic function paginate($perPage = 1, $columns = array('*'));\npublic function create(array $data)\n// if you use mongodb then you'll need to specify primary key $attribute\npublic function update(array $data, $id, $attribute = \"id\")\npublic function delete($id)\npublic function find($id, $columns = array('*'))\npublic function findBy($field, $value, $columns = array('*'))\npublic function findAllBy($field, $value, $columns = array('*'))\npublic function findWhere($where, $columns = array('*'))\n```\n\n##### Bosnadev\\Repositories\\Contracts\\CriteriaInterface\n\n```php\npublic function apply($model, Repository $repository)\n```\n\n### Example usage\n\n\nCreate a new film in repository:\n\n```php\n$this-\u003efilm-\u003ecreate(Input::all());\n```\n\nUpdate existing film:\n\n```php\n$this-\u003efilm-\u003eupdate(Input::all(), $film_id);\n```\n\nDelete film:\n\n```php\n$this-\u003efilm-\u003edelete($id);\n```\n\nFind film by film_id;\n\n```php\n$this-\u003efilm-\u003efind($id);\n```\n\nyou can also chose what columns to fetch:\n\n```php\n$this-\u003efilm-\u003efind($id, ['title', 'description', 'release_date']);\n```\n\nGet a single row by a single column criteria.\n\n```php\n$this-\u003efilm-\u003efindBy('title', $title);\n```\n\nOr you can get all rows by a single column criteria.\n```php\n$this-\u003efilm-\u003efindAllBy('author_id', $author_id);\n```\n\nGet all results by multiple fields\n\n```php\n$this-\u003efilm-\u003efindWhere([\n    'author_id' =\u003e $author_id,\n    ['year','\u003e',$year]\n]);\n```\n\n## Criteria\n\nCriteria is a simple way to apply specific condition, or set of conditions to the repository query. Your criteria class MUST extend the abstract ```Bosnadev\\Repositories\\Criteria\\Criteria``` class.\n\nHere is a simple criteria:\n\n```php\n\u003c?php namespace App\\Repositories\\Criteria\\Films;\n\nuse Bosnadev\\Repositories\\Criteria\\Criteria;\nuse Bosnadev\\Repositories\\Contracts\\RepositoryInterface as Repository;\n\nclass LengthOverTwoHours extends Criteria {\n\n    /**\n     * @param $model\n     * @param RepositoryInterface $repository\n     * @return mixed\n     */\n    public function apply($model, Repository $repository)\n    {\n        $model = $model-\u003ewhere('length', '\u003e', 120);\n        return $model;\n    }\n}\n```\n\nNow, inside you controller class you call pushCriteria method:\n\n```php\n\u003c?php namespace App\\Http\\Controllers;\n\nuse App\\Repositories\\Criteria\\Films\\LengthOverTwoHours;\nuse App\\Repositories\\FilmsRepository as Film;\n\nclass FilmsController extends Controller {\n\n    /**\n     * @var Film\n     */\n    private $film;\n\n    public function __construct(Film $film) {\n\n        $this-\u003efilm = $film;\n    }\n\n    public function index() {\n        $this-\u003efilm-\u003epushCriteria(new LengthOverTwoHours());\n        return \\Response::json($this-\u003efilm-\u003eall());\n    }\n}\n```\n\n\n## Credits\n\nThis package is largely inspired by [this](https://github.com/prettus/l5-repository) great package by @andersao. [Here](https://github.com/anlutro/laravel-repository/) is another package I used as reference.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbosnadev%2Frepository","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbosnadev%2Frepository","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbosnadev%2Frepository/lists"}