{"id":19691542,"url":"https://github.com/omnicode/lara-repo","last_synced_at":"2025-07-29T09:40:29.194Z","repository":{"id":57031368,"uuid":"96009865","full_name":"omnicode/lara-repo","owner":"omnicode","description":null,"archived":false,"fork":false,"pushed_at":"2018-10-15T06:46:27.000Z","size":116,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-29T09:43:00.622Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/omnicode.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":"2017-07-02T07:55:34.000Z","updated_at":"2024-07-26T09:37:31.000Z","dependencies_parsed_at":"2022-08-23T18:50:48.895Z","dependency_job_id":null,"html_url":"https://github.com/omnicode/lara-repo","commit_stats":null,"previous_names":[],"tags_count":44,"template":false,"template_full_name":null,"purl":"pkg:github/omnicode/lara-repo","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omnicode%2Flara-repo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omnicode%2Flara-repo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omnicode%2Flara-repo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omnicode%2Flara-repo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/omnicode","download_url":"https://codeload.github.com/omnicode/lara-repo/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/omnicode%2Flara-repo/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":265866139,"owners_count":23840937,"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":[],"created_at":"2024-11-11T19:09:45.118Z","updated_at":"2025-07-29T09:40:29.168Z","avatar_url":"https://github.com/omnicode.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n\u003cp align=\"center\"\u003e\n\u003ca href=\"https://travis-ci.org/omnicode/lara-repo\"\u003e\u003cimg src=\"https://travis-ci.org/omnicode/lara-repo.svg\" alt=\"Build Status\"\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/omnicode/lara-repo\"\u003e\u003cimg src=\"https://poser.pugx.org/omnicode/lara-repo/d/total.svg\" alt=\"Total Downloads\"\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/omnicode/lara-repo\"\u003e\u003cimg src=\"https://poser.pugx.org/omnicode/lara-repo/v/stable.svg\" alt=\"Latest Stable Version\"\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/omnicode/lara-repo\"\u003e\u003cimg src=\"https://poser.pugx.org/omnicode/lara-repo/license.svg\" alt=\"License\"\u003e\u003c/a\u003e\n\u003c/p\u003e\n\n## Installation\n\nRun the following command from you terminal:\n\n\n ```bash\n composer require \"omnicode/lara-repo: 2.0.*\"\n ```\n\nor add this to require section in your composer.json file:\n\n ```\n \"omnicode/lara-repo\": \"2.0.*\"\n ```\n\nthen run ```composer update```\n\n\n## Usage\n\n\nFirst, create your RepositoryInterface interface that should extend\n```LaraRepo\\Contracts\\RepositoryInterface``` \n\ne.g.\n\n```php\n\u003c?php\n\nnamespace App\\Repositories\\Contracts;\n\nuse LaraRepo\\Contracts\\RepositoryInterface;\n\ninterface AccountRepositoryInterface extends RepositoryInterface\n{\n    \n}\n\n```\n\nNext, create your repository class. Note that your repository class should extend ```LaraRepo\\Eloquent\\AbstractRepository```  and have ```modelClass()``` method\n\ne.g.\n```php\n\u003c?php\n\nnamespace App\\Repositories\\Eloquent;\n\nuse LaraRepo\\Eloquent\\AbstractRepository;\nuse App\\Repositories\\Contracts\\AccountRepositoryInterface;\nuse App\\Models\\Account;\n\nclass AccountRepository extends AbstractRepository  implements AccountRepositoryInterface\n{\n    public function modelClass()\n    {\n        return Account::class;\n    }\n}\n```\n\n```modelClass()``` method is used to identify the model class\n\n\nIt is suggested to use LaraModel repository to utilize all the functionality\n\n ```\n \"omnicode/lara-model\": \"2.0.*\"\n ```\nand use\n\nAnd you can create ```App\\Models\\Account``` model\n\n```php\n\u003c?php\n\nnamespace App\\Models;\n\nuse LaraModel\\Models\\LaraModel;\n\nclass Account extends LaraModel\n{\n    \n}\n```\n\n\nNext Bind it in ServiceProvider.\n\n```php\n\u003c?php\n\nnamespace App\\Providers;\n\nuse App\\Repositories\\Contracts\\AccountUserRepositoryInterface;\nuse App\\Repositories\\Eloquent\\AccountRepository;\nuse Illuminate\\Support\\ServiceProvider;\n\nclass AppServiceProvider extends ServiceProvider\n{\n    /**\n     * Register any application services.\n     *\n     * @return void\n     */\n    public function register()\n    {\n        $this-\u003eapp-\u003ebind(AccountUserRepositoryInterface::class, AccountRepository::class);\n    }\n}\n\n```\n\n\nAnd finally, use the repository in your controller or service layer\n\n```php\n\u003c?php\n\nnamespace App\\Http\\Controllers;\n\nuse App\\Repositories\\Contracts\\AccountRepositoryInterface as AccountRepository;\n\nclass AccountsController extends Controller\n{\n\n    /**\n     * @var AccountRepository\n     */\n    protected $accountRepository;\n\n    /**\n     * AccountsController constructor.\n     * @param AccountRepository $accountRepository\n     */\n    public function __construct(AccountRepository $accountRepository)\n    {\n        $this-\u003eaccountRepository = $accountRepository;\n    }\n\n    /**\n     * @return \\Illuminate\\Contracts\\View\\Factory|\\Illuminate\\View\\View\n     */\n    public function index()\n    {\n        $items = $this-\u003eaccountRepository-\u003eall(); \n        return view('accounts.index', compact('items'));\n    }\n}\n\n```\n\n## Available Methods\n\nThe following methods are available:\n\n##### LaraRepo\\Contracts\\RepositoryInterface\n\n```php\n    public function makeModel();\n    public function getModel();\n    public function getModelQuery();\n    public function resetModelQuery();\n    public function getTable();\n    public function getKeyName();\n    public function fixColumns($columns, $table = null, $prefix = null);\n    public function getFillableColumns();\n    public function getIndexableColumns($full = false, $hidden = true, $group = self::GROUP);\n    public function getShowableColumns($full = false, $hidden = true, $group = self::GROUP);\n    public function getSearchableColumns();\n    public function getListableColumns();\n    public function getSortableColumns($column = null, $group = self::GROUP);\n    public function getStatusColumn();\n    public function setSortingOptions($column = null, $order = 'asc', $group = self::GROUP);\n    public function getRelations();\n    public function saveAssociated($data, $options = [], $model = null);\n    public function create(array $data);\n    public function createWith(array $data, $attribute, $value);\n    public function increment($column, $value);\n    public function update(array $data, $id, $attribute = \"id\");\n    public function updateBased(array $data, array $conditions);\n    public function destroy($id);\n    public function destroyBy($attribute, $value);\n    public function all($columns = null);\n    public function first($columns = null);\n    public function last($columns = null);\n    public function find($id, $columns = null);\n    public function findForShow($id, $columns = null);\n    public function findBy($attribute, $value, $columns = null);\n    public function findAllBy($attribute, $value, $columns = null);\n    public function findAttribute($id, $attribute);\n    public function findFillable($id);\n    public function findAllFillable($attribute, $value);\n    public function findFillableWith($id, $related = []);\n    public function findFillableWhere($id, $field, $value, $cmp = '=');\n    public function findList($active = true, $listable = null);\n    public function findListBy($attribute, $value, $active = true, $listable = null);\n    public function paginate($perPage = 15, $columns = null, $group = self::GROUP);\n    public function paginateWhere($attribute = '', $value = '', $cmp = '=');\n    public function findCount($attribute = null, $value = null, $cmp = '=');\n    public function exists($id);\n    public function existsWhere($attribute, $value);\n}\n```\n\n##### LaraRepo\\Contracts\\CriteriaInterface\n\n```php\n    public function getCriteria();\n    public function getByCriteria(Criteria $criteria);\n    public function resetScope();\n    public function pushCriteria(Criteria $criteria);\n    public function skipCriteria($status = true);\n    public function applyCriteria();\n```\n\n##### LaraRepo\\Contracts\\TransactionInterface\n\n```php\n    public function startTransaction();\n    public function commitTransaction();\n    public function rollbackTransaction();\n```\n\n### Example usage\n\n\nCreate a new account repository:\n\ne.g.\n```php\n    \n    $this-\u003eaccountRepository-\u003ecreate($request-\u003eall());\n    \n    $this-\u003eaccountRepository-\u003ecreateWith($request-\u003eall(), 'name', $name);\n    \n    it is equivalent \n    \n    $data = array_merge($request-\u003eall(), ['name' =\u003e $name])\n    $this-\u003eaccountRepository-\u003ecreate($data);\n    \n    //if your model extends LaraModel/Models/LaraModel you can use saveAssociated Method for saving new account\n    //or update existing model with relations\n    $this-\u003eaccountRepository-\u003esaveAssociated($data, $relations);\n    \n    //for updating with relations\n    $this-\u003eaccountRepository-\u003esaveAssociated($data, $relations, $account);\n```\n\nUpdate existing account:\n\n```php\n    // update based on id\n    $this-\u003eaccountRepository-\u003eupdate($request-\u003eall(), $accountId);\n\n    // update based on attribute\n    $this-\u003eaccountRepository-\u003eupdate($request-\u003eall(), $attribute, 'attribute');\n    \n    // for increment attribute with value\n    $this-\u003eaccountRepository-\u003eincrement($attribute, $value);\n    \n    //for decrement attribute with value\n    $this-\u003eaccountRepository-\u003edecrement($attribute, $value);\n```\n\nDelete account:\n\n```php\n    // delete by id\n    $this-\u003eaccountRepository-\u003edestroy($accountId);\n```\n\nFind account(s) and specify columns with you want to fetch\n\n```php\n    // find by accountId\n    $this-\u003eaccountRepository-\u003efind($accountId, $columns);\n    \n    // find by attribute (first occurence)\n    $this-\u003eaccountRepository-\u003efindBy($attribute, $value, $columns);\n    \n    // find by attribute (all occurence)\n    $this-\u003eaccountRepository-\u003efindAllBy($attribute, $value, $columns);\n    \n    // to find and return exact attribute \n    $this-\u003eaccountRepository-\u003efindAttribute($accountId, $attribute);\n    \n    // to find account by accountId with fillable columns\n    $this-\u003eaccountRepository-\u003efindFillable($accountId);\n    \n    // to find account by attribute with fillable columns\n    $this-\u003eaccountRepository-\u003efindFillableWhere($id, $attribute, $value, $cmp = '=')\n\n    // to find all accounts by attribute with fillable columns use\n    $this-\u003eaccountRepository-\u003efindAllFillable($attribute, $value);\n\n    // to find account with relations\n    $this-\u003eaccountRepository-\u003efindFillableWith($id, $related = [])\n    \n\n    // to find all accounts\n    $this-\u003eaccountRepository-\u003eall($columns)\n    \n    // to find first account\n    $this-\u003eaccountRepository-\u003efirst($columns = null)\n    \n    //for find last account\n    $this-\u003eaccountRepository-\u003elast($columns = null)\n    \n    // if your model extends LaraModel\\Models\\LaraModel\n    // and $showable property is specified you can use\n    $this-\u003eaccountRepository-\u003efindForShow($id, $columns = null)\n    \n    // if your model extends LaraModel\\Models\\LaraModel\n    // and specifed $listable property you can use\n    // for find accounts list \n    //[\n    //      1 =\u003e 'account1',\n    //      2 =\u003e 'account2'\n    //]\n    //\n    $this-\u003eaccountRepository-\u003efindList($active = true, $listable = null)\n    \n    // or you can use to find by attribute\n    $this-\u003eaccountRepository-\u003efindListBy($attribute, $value, $active = true, $listable = null)\n    \n    \n    // to check if account exists with id\n    $this-\u003eaccountRepository-\u003eexists($id);\n    \n    // to find count of rows matching the given critera\n    $this-\u003eaccountRepository-\u003efindCount($attribute, $value, $cmp);\n    \n    // to check if item exists with given attribute and value\n    $this-\u003eaccountRepository-\u003eexistsWhere($attribute, $value);\n    \n    // for pagination\n    $this-\u003eaccountRepository-\u003epaginate();\n    \n    // for pagination based by condition\n    $this-\u003eaccountRepository-\u003epaginateWhere($attribute, $value);\n```\n\nmodel related methods\n\n```php\n\n    // to get the binded model    \n    $this-\u003eaccount-\u003egetModel();\n\n    // get current model query\n    $this-\u003eaccount-\u003egetModelQuery();\n\n    // to reset model query    \n    $this-\u003eaccount-\u003eresetModelQuery();\n\n    // to get model's table name\n    $this-\u003eaccount-\u003egetTable();\n\n    // to get model table's primary key\n    $this-\u003eaccount-\u003egetKeyName();\n\n    // to get model's fillable columns\n    $this-\u003eaccount-\u003egetFillableColumns();\n\n```\nif your model extends LaraModel\\Models\\LaraModel you can also use this methods\n```php\n    // returns columns including table name\n    $this-\u003eaccountRepository-\u003efixColumns($columns, $table = null, $prefix = null);\n\n    // returns indexable columns\n    $this-\u003eaccount-\u003egetIndexableColumns($full = false, $hidden = true, $group = self::GROUP);\n\n    // returns columns based on $showable property\n    $this-\u003eaccountRepository-\u003egetShowableColumns($full = false, $hidden = true, $group = self::GROUP);\n\n    // returns serchable columns\n    $this-\u003eaccountRepository-\u003egetSearchableColumns();\n\n    // returns listable columns\n    $this-\u003eaccountRepository-\u003egetListableColumns();\n\n    // returns sortable columns\n    $this-\u003eaccountRepository-\u003egetSortableColumns($column = null, $group = self::GROUP);\n\n    // returns status column\n    $this-\u003eaccountRepository-\u003egetStatusColumn();\n\n    // add sorting options in columns\n    $this-\u003eaccountRepository-\u003esetSortingOptions($column = null, $order = 'asc', $group = self::GROUP);\n\n    // returns model relations\n    $this-\u003eaccountRepository-\u003egetRelations();\n\n```\n\nCriteria methods\n```php\n\n    $this-\u003eaccountRepository-\u003egetCriteria();\n\n    $this-\u003eaccountRepository-\u003egetByCriteria(Criteria $criteria);\n\n    $this-\u003eaccountRepository-\u003eresetScope();\n\n    $this-\u003eaccountRepository-\u003epushCriteria(Criteria $criteria);\n\n    $this-\u003eaccountRepository-\u003eskipCriteria($status = true);\n\n    $this-\u003eaccountRepository-\u003eapplyCriteria();\n```\n\nTransaction methods\n```php\n\n    $this-\u003eaccountRepository-\u003estartTransaction();\n    \n    $this-\u003eaccountRepository-\u003ecommitTransaction();\n    \n    $this-\u003eaccountRepository-\u003erollbackTransaction();\n```\n\n\n##### LaraRepo\\Contracts\\Criteria\\Criteria\n\n```php\n    public abstract function apply($modelQuery, RepositoryInterface $repository);\n```\n\n## Criteria\n\nCriteria is a simple way to apply specific condition, or set of conditions to the query\n\n## Available Criteria\n\nThe following criteria are available:\n\n```php\n    LaraRepo\\Criteria\\Distinct\\DistinctCriteria\n    LaraRepo\\Criteria\\Group\\GroupByCriteria             __construct($columns)\n    LaraRepo\\Criteria\\Has\\HasCriteria                   __construct($columns, $value, $cmp = '=')\n    LaraRepo\\Criteria\\Join\\InnerJoinCriteria            __construct($relation, $column = '', $values = [])\n    LaraRepo\\Criteria\\Join\\InnerJoinRelationCriteria    __construct($relation, $otherKeys = [])\n    LaraRepo\\Criteria\\Join\\LeftJoinCriteria             __construct($relation, $column = '', $values = [])\n    LaraRepo\\Criteria\\Limit\\LimitCriteria               __construct($limit)\n    LaraRepo\\Criteria\\Offset\\OffsetCriteria             __construct($offset)\n    LaraRepo\\Criteria\\Order\\SortCriteria                __construct($column, $order = 'asc', $fixColumns = true)\n    LaraRepo\\Criteria\\Search\\SearchCriteria             __construct($value, $columns = null, $table = null)\n    LaraRepo\\Criteria\\Select\\SelectCriteria             __construct($columns = [], $table = null)\n    LaraRepo\\Criteria\\Select\\SelectFillableCriteria     __construct($include = [], $exclude = [])             \n    LaraRepo\\Criteria\\Select\\SelectCriteria             __construct($relation, $columns, $prefix = true)\n    LaraRepo\\Criteria\\Where\\ActiveCriteria\n    LaraRepo\\Criteria\\Where\\BetweenCriteria             __construct($column, $from, $to)\n    LaraRepo\\Criteria\\Where\\OrWhereCriteria             __construct($attribute, $value, $cmp = '=') \n    LaraRepo\\Criteria\\Where\\WhereCriteria               __construct($attribute, $value, $cmp = '=')\n    LaraRepo\\Criteria\\Where\\WhereHasRelationCriteria    __construct($relation, $where = [])\n    LaraRepo\\Criteria\\Where\\WhereInCriteria             __construct($attribute, $values = [])\n    LaraRepo\\Criteria\\Where\\WhereRelationCriteria       __construct($relation, $attribute, $value, $cmp = '=')\n    LaraRepo\\Criteria\\With\\RelationCriteria             __construct($relations = [])\n    LaraRepo\\Criteria\\With\\WithCountCriteria             __construct($relations)\n```\n\nYou can make your own Criteria. Your criteria class MUST extend the abstract ```LaraRepo\\Criteria\\Criteria``` class.\n\nFor exapmle:\n```php\n\u003c?php\nnamespace YourNamespace;\n\nuse LaraRepo\\Contracts\\RepositoryInterface;\nuse LaraRepo\\Criteria\\Criteria;\n\nclass NewCriteria extends Criteria\n{\n    \n    public function __construct()\n    {\n        //your code\n    }\n\n    /***\n     * @param $modelQuery\n     * @param RepositoryInterface $repository\n     * @return mixed\n     */\n    public function apply($modelQuery, RepositoryInterface $repository)\n    {\n        //your code here\n        return $modelQuery;\n    }\n\n}\n\n```\n\nNow, inside you controller class you call pushCriteria method:\n\n```php\n\u003c?php\n\nnamespace App\\Http\\Controllers;\n\nuse LaraRepo\\Criteria\\Where\\WhereCriteria;\nuse App\\Repositories\\Contracts\\AccountRepositoryInterface as AccountRepository;\n\nclass AccountsController extends Controller\n{\n\n    /**\n     * @var AccountRepository\n     */\n    protected $repository;\n\n    /**\n     * AccountsController constructor.\n     * @param AccountRepository $accountRepository\n     */\n    public function __construct(AccountRepository $accountRepository)\n    {\n        $this-\u003erepository = $accountRepository;\n    }\n\n    /**\n     * @return \\Illuminate\\Contracts\\View\\Factory|\\Illuminate\\View\\View\n     */\n    public function index()\n    {\n        $this-\u003erepository-\u003epushCriteria(new WhereCriteria('id', '15', '\u003e'));\n        $items = $this-\u003erepository-\u003eall(); \n        return view('accounts.index', compact('items'));\n    }\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\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fomnicode%2Flara-repo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fomnicode%2Flara-repo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fomnicode%2Flara-repo/lists"}