{"id":19638007,"url":"https://github.com/simon28082/repository","last_synced_at":"2025-08-21T11:14:02.183Z","repository":{"id":56958841,"uuid":"75898581","full_name":"simon28082/repository","owner":"simon28082","description":"Detached model and controller warehouse data provider","archived":false,"fork":false,"pushed_at":"2020-06-10T08:50:34.000Z","size":248,"stargazers_count":67,"open_issues_count":0,"forks_count":12,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-05-06T14:17:05.230Z","etag":null,"topics":["laravel","model","orm","repository"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/simon28082.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null},"funding":{"github":null,"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":["https://blog.crcms.cn/reward/"]}},"created_at":"2016-12-08T03:16:57.000Z","updated_at":"2025-01-23T17:52:42.000Z","dependencies_parsed_at":"2022-08-21T09:50:30.024Z","dependency_job_id":null,"html_url":"https://github.com/simon28082/repository","commit_stats":null,"previous_names":[],"tags_count":25,"template":false,"template_full_name":null,"purl":"pkg:github/simon28082/repository","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simon28082%2Frepository","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simon28082%2Frepository/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simon28082%2Frepository/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simon28082%2Frepository/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/simon28082","download_url":"https://codeload.github.com/simon28082/repository/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/simon28082%2Frepository/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":271469209,"owners_count":24765124,"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","status":"online","status_checked_at":"2025-08-21T02:00:08.990Z","response_time":74,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["laravel","model","orm","repository"],"created_at":"2024-11-11T12:37:22.451Z","updated_at":"2025-08-21T11:14:02.120Z","avatar_url":"https://github.com/simon28082.png","language":"PHP","funding_links":["https://blog.crcms.cn/reward/"],"categories":[],"sub_categories":[],"readme":"## CRCMS Repository\n\n[![Latest Stable Version](https://poser.pugx.org/crcms/repository/v/stable)](https://packagist.org/packages/crcms/repository)\n[![License](https://poser.pugx.org/crcms/repository/license)](https://packagist.org/packages/crcms/repository)\n[![StyleCI](https://github.styleci.io/repos/75898581/shield?branch=master)](https://github.styleci.io/repos/75898581)\n\nA specialized data provider layer, based on ORM, only as a data provider, the main role is to separate the coupling before the Controller and Model\n\n## Install\n\nYou can install the package via composer:\n\n```bash\ncomposer require crcms/repository\n```\n\n## Laravel\n\nIf your version is less than 5.5 please modify ``config / app.php``\n\n```php\n'providers' =\u003e [\n    CrCms\\Repository\\RepositoryServiceProvider::class,\n]\n\n```\n\nIf you'd like to make configuration changes in the configuration file you can pubish it with the following Aritsan command:\n```bash\nphp artisan vendor:publish --provider=\"CrCms\\Repository\\RepositoryServiceProvider\"\n```\n\n## Commands\n\n```bash\nphp artisan make:repository TestRepository --model TestModel\n```\n```bash\nphp artisan make:magic TestMagic\n```\n\n## Example\n\n### QueryMagic\n```php\n\nuse CrCms\\Repository\\AbstractMagic;\nuse CrCms\\Repository\\Contracts\\QueryRelate;\n\nclass TestMagic extends AbstractMagic\n{\n    /**\n     * @param QueryRelate $queryRelate\n     * @param int $id\n     * @return QueryRelate\n     */\n    protected function byName(QueryRelate $queryRelate, string $name)\n    {\n        return $queryRelate-\u003ewhere('name', $name);\n    }\n\n    /**\n     * @param QueryRelate $queryRelate\n     * @param string $title\n     * @return QueryRelate\n     */\n    protected function byTitle(QueryRelate $queryRelate, string $title)\n    {\n        return $queryRelate-\u003ewhere('title', 'like', \"%{$title}%\");\n    }\n\n    /**\n     * @param QueryRelate $queryRelate\n     * @param int $id\n     * @return QueryRelate\n     */\n    protected function byId(QueryRelate $queryRelate, int $id)\n    {\n        return $queryRelate-\u003ewhere('id', $id);\n    }\n    \n    /**\n     * @param QueryRelate $queryRelate\n     * @param array $sort\n     * @return QueryRelate\n     */\n    protected function bySort(QueryRelate $queryRelate, array $sort)\n    {\n        return $queryRelate-\u003eorderByArray($sort);\n    }\n}\n```\n\n### Repository\n```php\nclass TestRepository extends AbstractRepository\n{\n    /**\n     * @var array\n     */\n    protected $guard = [\n        'id', 'title','other'\n    ];\n\n    /**\n     * @return TestModel\n     */\n    public function newModel(): TestModel\n    {\n        return app(TestModel::class);\n    }\n\n    /**\n     * @param int $perPage\n     * @return LengthAwarePaginator\n     */\n    public function paginate(AbstractMagic $magic = null, int $perPage = 15): LengthAwarePaginator\n    {\n        return $this-\u003ewhenMagic($magic)-\u003ewhere('built_in', 1)-\u003eorderBy($this-\u003egetModel()-\u003egetKeyName(), 'desc')-\u003epaginate($perPage);\n    }\n\n    /**\n     * @param int $name\n     * @param int $title\n     */\n    public function updateName(string $name, string $title)\n    {\n        $this-\u003ewhere('name', $name)-\u003eupdate(['title' =\u003e $title]);\n    }\n    \n}\n```\n\n### Guard Or Scenes\n\nUsually we need to filter the incoming parameter values when adding or modifying and querying the data, and retain the required parameter values.\n\nGuard and scenes are born for this\n\n```php\nclass TestRepository extends AbstractRepository\n{\n    /**\n     * @var array\n     */\n    protected $scenes = [\n        'create' =\u003e ['sort', 'added_at'],\n        'modify' =\u003e ['sort', 'published_at']\n    ];\n    \n    /**\n     * @var array\n     */\n    protected $guard = [\n        'id', 'title', 'other'\n    ];\n}\n\n$testRepository-\u003ecreate($data, 'create'); //OR\n$testRepository-\u003esetCurrentScene('create')-\u003ecreate($data); //OR\n$testRepository-\u003esetGuard(['sort', 'added_at'])-\u003ecreate($data); \n\n```\n\n```php\nclass TestMagic extends AbstractMagic\n{\n    /**\n     * @var array\n     */\n    protected $scenes = [\n        'frontend' =\u003e ['name'],\n        'backend' =\u003e ['title']\n    ];\n    \n    /**\n     * @var array\n     */\n    protected $guard = [\n        'title',\n    ];\n\n    /**\n     * @param QueryRelate $queryRelate\n     * @param int $id\n     * @return QueryRelate\n     */\n    protected function byName(QueryRelate $queryRelate, string $name)\n    {\n        return $queryRelate-\u003ewhere('name', $name);\n    }\n\n    /**\n     * @param QueryRelate $queryRelate\n     * @param string $title\n     * @return QueryRelate\n     */\n    protected function byTitle(QueryRelate $queryRelate, string $title)\n    {\n        return $queryRelate-\u003ewhere('title', 'like', \"%{$title}%\");\n    }\n}\n\n$testRepository-\u003emagic(new TestMagic($data, 'frontend'))-\u003epaginate(); //OR\n$testRepository-\u003emagic((new TestMagic($data))-\u003esetCurrentScene('frontend'))-\u003epaginate(); //OR\n$testRepository-\u003emagic((new TestMagic($data))-\u003esetGuard(['title']))-\u003epaginate(); //OR-\u003ecreate($data);\n\n```\n\n**Note: when guard and scenes are both present, scenes has a higher priority. If scenes is empty, it will use guard.**\n\n### Listener\n\n```php\nTestRepository::observer(TestListener::class);\n\nTestListener {\n\n    public function creating(TestRepository $repository, array $data)\n    {\n\t\t//append the value to be written\n\t\t$repository-\u003eaddData('append_data','value');\n\t\t\n\t\t//rewrite all values written\n\t\t$repository-\u003esetData(['key'=\u003e'value']);\n    }    \n    \n    public function created(TestRepository $repository, TestModel $model)\n    {\n    }    \n\n    public function updating(TestRepository $repository, array $data)\n    {\n    }    \n    \n    public function updated(TestRepository $repository, TestModel $model)\n    {\n    }\n    \n    public function deleting(TestRepository $repository, array $ids)\n    {\n    }\n    \n    public function deleted(TestRepository $repository, Collection $models)\n    {\n    }\n}\n```\n\n### Cache\n\n```php\nclass TestRepository {\n\n    public function do(User $user)\n    {\n        return $this-\u003ebyIntId($user-\u003eid);\n    }\n}\n\n$repository = new TestRepository;\n\n```\n\n#### store\n```php\n$repository-\u003ecache()-\u003edo(new User);\n```\n\n#### forget\n```php\n$repository-\u003ecache()-\u003eforget('do')\n```\n\n#### flush\n```php\n$repository-\u003ecache()-\u003eflush()\n```\n\n\n### Repository Methods\n```php\npublic function all(): Collection;\n```\n```php\npublic function get(): Collection;\n``` \n```php\npublic function pluck(string $column, string $key = ''): Collection;\n```\n```php\npublic function max(string $column): int;\n```\n```php\npublic function count(string $column = '*'): int;\n```\n```php\npublic function avg($column): int;\n```\n```php\npublic function sum(string $column): int;\n```\n```php\npublic function chunk(int $limit, callable $callback): bool;\n```\n```php\npublic function valueOfString(string $key, string $default = ''): string;\n```\n```php\npublic function valueOfInt(string $key, int $default = 0): int;\n```\n```php\npublic function increment(string $column, int $amount = 1, array $extra = []): int;\n```\n```php\npublic function decrement(string $column, int $amount = 1, array $extra = []): int;\n```\n```php\npublic function delete(): int;\n```\n```php\npublic function deleteByStringId(string $id): int;\n```\n```php\npublic function deleteByIntId(int $id): int;\n```\n```php\npublic function deleteByArray(array $ids): int;\n```\n```php\npublic function paginate(int $perPage = 15) : LengthAwarePaginator;\n```\n```php\npublic function create(array $data) : Model;\n```\n```php\npublic function update(array $data): int;\n```\n```php\npublic function updateByIntId(array $data, int $id) : Model;\n```\n```php\npublic function updateByStringId(array $data, string $id) : Model;\n```\n```php\npublic function byIntId(int $id);\n```\n```php\npublic function byStringId(string $id);\n```\n```php\npublic function byIntIdOrFail(int $id) : Model;\n```\n```php\npublic function byStringIdOrFail(string $id) : Model;\n```\n```php\npublic function oneByString(string $field, string $value): Model;\n```\n```php\npublic function oneByInt(string $field, int $value): Model;\n```\n```php\npublic function oneByStringOrFail(string $field, string $value) : Model;\n```\n```php\npublic function oneByIntOrFail(string $field, int $value) : Model;\n```\n```php\npublic function first();\n```\n```php\npublic function firstOrFail() : Model;\n```    \n\n### QueryRelate Methods\n\n```php\npublic function select(array $column = ['*']): QueryRelate;\n```\n```php\npublic function selectRaw(string $expression, array $bindings = []): QueryRelate;\n```\n```php\npublic function skip(int $limit): QueryRelate;\n```\n```php\npublic function take(int $limit): QueryRelate;\n```\n```php\npublic function groupBy(string $column): QueryRelate;\n```\n```php\npublic function groupByArray(array $columns): QueryRelate;\n```\n```php\npublic function orderBy(string $column, string $sort = 'desc'): QueryRelate;\n```\n```php\npublic function orderByArray(array $columns): QueryRelate;\n```\n```php\npublic function distinct(): QueryRelate;\n```\n```php\npublic function where(string $column, string $operator = '=', string $value = ''): QueryRelate;\n```\n```php\npublic function whereClosure(\\Closure $callback): QueryRelate;\n```\n```php\npublic function orWhereClosure(\\Closure $callback): QueryRelate;\n```\n```php\npublic function orWhere(string $column, string $operator = '=', string $value = ''): QueryRelate;\n```    \n```php\npublic function whereBetween(string $column, array $between): QueryRelate;\n```\n```php\npublic function orWhereBetween(string $column, array $between): QueryRelate;\n```    \n```php\npublic function whereRaw(string $sql, array $bindings = []): QueryRelate;\n```\n```php\npublic function orWhereRaw(string $sql, array $bindings = []): QueryRelate;\n```\n\n```php\npublic function orWhereNotBetween($column, array $between): QueryRelate;\n```    \n\n```php\npublic function whereExists(\\Closure $callback): QueryRelate;\n```\n\n```php\npublic function orWhereExists(\\Closure $callback): QueryRelate;\n```\n\n```php\npublic function whereNotExists(\\Closure $callback): QueryRelate;\n```\n\n```php\npublic function orWhereNotExists(\\Closure $callback): QueryRelate;\n```\n```php\npublic function whereIn(string $column, array $values): QueryRelate;\n```\n```php\npublic function orWhereIn(string $column, array $values): QueryRelate;\n```\n```php\npublic function whereNotIn(string $column, array $values): QueryRelate;\n```    \n\n```php\npublic function orWhereNotIn(string $column, array $values): QueryRelate;\n```    \n```php\npublic function whereNull(string $column): QueryRelate;\n```\n```php\npublic function orWhereNull(string $column): QueryRelate;\n```\n```php\npublic function whereNotNull(string $column): QueryRelate;\n```\n```php\npublic function orWhereNotNull(string $column): QueryRelate;\n```\n```php\npublic function raw(string $sql): QueryRelate;\n```    \n```php\npublic function from(string $table): QueryRelate;\n```\n```php\npublic function join(string $table, string $one, string $operator = '=', string $two = ''): QueryRelate;\n```\n```php\npublic function joinClosure(string $table, \\Closure $callback): QueryRelate;\n```\n```php\npublic function leftJoin(string $table, string $first, string $operator = '=', string $two = ''): QueryRelate;\n```\n```php\npublic function leftJoinClosure(string $table, \\Closure $callback): QueryRelate;\n```\n```php\npublic function rightJoin(string $table, string $first, string $operator = '=', string $two = ''): QueryRelate;\n```\n```php\npublic function rightJoinClosure(string $table, \\Closure $callback): QueryRelate;\n```\n```php\npublic function callable(callable $callable): QueryRelate;\n```\n```php\npublic function wheres(array $wheres): QueryRelate;\n```    \n```php\npublic function union(QueryRelate $queryRelate): QueryRelate;\n```\n```php\npublic function magic(QueryMagic $queryMagic): QueryRelate;\n```\n```php\npublic function whenMagic(?QueryMagic $queryMagic = null): QueryRelate;\n```\n```php\npublic function with(string $relation): QueryRelate;\n```\n```php\npublic function withArray(array $relations): QueryRelate;\n```\n```php\npublic function without(string $relation): QueryRelate;\n```\n```php\npublic function withoutArray(array $relations): QueryRelate;\n```\n```php\npublic function having(string $column, $operator = null, $value = null): QueryRelate;\n```\n```php\npublic function orHaving(string $column, $operator = null, $value = null): QueryRelate;\n```\n```php\npublic function havingRaw(string $sql, array $bindings = []): QueryRelate;\n```\n```php\npublic function orHavingRaw(string $sql, array $bindings = []): QueryRelate;\n```\n```php\npublic function lockForUpdate(): QueryRelate;\n```\n```php\npublic function sharedLock(): QueryRelate;\n```\n\n\n## License\n[MIT license](https://opensource.org/licenses/MIT)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimon28082%2Frepository","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsimon28082%2Frepository","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsimon28082%2Frepository/lists"}