{"id":16314778,"url":"https://github.com/forecho/laravel-repository","last_synced_at":"2025-03-22T20:36:06.734Z","repository":{"id":58488573,"uuid":"531802465","full_name":"forecho/laravel-repository","owner":"forecho","description":"A generic repository implementation for Laravel with a few extras.","archived":false,"fork":false,"pushed_at":"2024-03-02T14:27:13.000Z","size":29,"stargazers_count":5,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-18T14:53:34.424Z","etag":null,"topics":["laravel","laravel-package","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/forecho.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-09-02T06:07:23.000Z","updated_at":"2023-03-17T13:13:26.000Z","dependencies_parsed_at":"2024-10-28T14:46:25.353Z","dependency_job_id":"3042514f-4913-4b2c-9b7e-78ffe16ef610","html_url":"https://github.com/forecho/laravel-repository","commit_stats":{"total_commits":18,"total_committers":1,"mean_commits":18.0,"dds":0.0,"last_synced_commit":"3faea89dbb16e5e77bff463b74c36f468c1e5dd0"},"previous_names":[],"tags_count":11,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/forecho%2Flaravel-repository","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/forecho%2Flaravel-repository/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/forecho%2Flaravel-repository/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/forecho%2Flaravel-repository/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/forecho","download_url":"https://codeload.github.com/forecho/laravel-repository/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245020181,"owners_count":20548155,"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":["laravel","laravel-package","repository"],"created_at":"2024-10-10T21:55:15.464Z","updated_at":"2025-03-22T20:36:06.320Z","avatar_url":"https://github.com/forecho.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Laravel Repository\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/forecho/laravel-repository.svg?style=flat-square)](https://packagist.org/packages/forecho/laravel-repository)\n[![Total Downloads](https://img.shields.io/packagist/dt/forecho/laravel-repository.svg?style=flat-square)](https://packagist.org/packages/forecho/laravel-repository)\n![GitHub Actions](https://github.com/forecho/laravel-repository/actions/workflows/main.yml/badge.svg)\n\nThe Laravel Repository package is meant to be a generic repository implementation for Laravel.\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require forecho/laravel-repository\n```\n\n## Usage\n\nCreate a repository class:\n\n```php\n\u003c?php \nnamespace App\\Repositories;\n\nuse App\\Models\\Post;\nuse Forecho\\LaravelRepository\\Repository;\n\nclass PostRepository extends Repository\n{\n    // Optional\n    public array $partialMatchAttributes = ['title'];\n\n    // Optional\n    public array $defaultOrder = ['created_at' =\u003e 'desc'];\n    \n    // Optional\n    public array $booleanAttributes = ['status'];\n\n    public string $modelClass = Post::class;\n\n    // Optional\n    public function boot()\n    {\n        $this-\u003epushCriteria(UserCriteriaCriteria::class);\n    }\n\n}\n```\n\nUse the repository in your controller:\n\n```php\n\u003c?php\nnamespace App\\Http\\Controllers;\n\nuse App\\Repositories\\PostRepository;\nuse Illuminate\\Http\\Request;\nuse App\\Http\\Resources\\UserResource;\n\nclass UserController extends Controller\n{\n    protected PostRepository $repository;\n\n    public function __construct(PostRepository $accountRepository)\n    {\n        $this-\u003erepository = $accountRepository;\n    }\n   \n    public function index(Request $request, PostRepository $userRepository)\n    {\n        $posts = $this-\u003erepository-\u003esearch($request-\u003eall())-\u003epaginate();\n        // $posts = $this-\u003erepository-\u003ewith(['user'])-\u003esearch($request-\u003eall())-\u003epaginate();\n        \n        return response()-\u003ejson(UserResource::collection($posts);\n    }\n}\n```\n\n## Criteria\n\nCreate a criteria class:\n\n```php\n\u003c?php\nnamespace App\\Criteria;\n\nuse App\\Services\\UserService;\nuse Forecho\\LaravelRepository\\Contracts\\CriteriaInterface;\nuse Forecho\\LaravelRepository\\Contracts\\RepositoryInterface;\nuse Illuminate\\Database\\Eloquent\\Builder;\nuse Illuminate\\Database\\Eloquent\\Model;\n\nclass UserCriteriaCriteria implements CriteriaInterface\n{\n    /**\n     * Apply criteria in query repository\n     *\n     * @param  Builder|Model  $model\n     * @param  RepositoryInterface  $repository\n     * @return mixed\n     */\n    public function apply($model, RepositoryInterface $repository): mixed\n    {\n        return $model-\u003ewhere('user_id', UserService::getUserId());\n    }\n}\n```\n\nRequest all data without filter by request:\n\nhttp://127.0.0.1:8000/api/posts\n\n```json\n[\n    {\n        \"id\": 1,\n        \"title\": \"title\",\n        \"content\": \"content\",\n        \"status\": 0,\n        \"created_at\": \"2021-08-01T07:00:00.000000Z\",\n        \"updated_at\": \"2021-08-01T07:00:00.000000Z\"\n    },\n    {\n        \"id\": 2,\n        \"title\": \"for echo\",\n        \"content\": \"content\",\n        \"status\": 1,\n        \"created_at\": \"2021-08-01T07:00:00.000000Z\",\n        \"updated_at\": \"2021-08-01T07:00:00.000000Z\"\n    }\n]\n```\n\nhttp://127.0.0.1:8000/api/posts?title=for\n\n```json\n[\n    {\n        \"id\": 2,\n        \"title\": \"for echo\",\n        \"content\": \"content\",\n        \"status\": 1,\n        \"created_at\": \"2021-08-01T07:00:00.000000Z\",\n        \"updated_at\": \"2021-08-01T07:00:00.000000Z\"\n    }\n]\n```\n\nhttp://127.0.0.1:8000/api/posts?status=0\n\n```json\n[\n    {\n        \"id\": 1,\n        \"title\": \"title\",\n        \"content\": \"content\",\n        \"status\": 0,\n        \"created_at\": \"2021-08-01T07:00:00.000000Z\",\n        \"updated_at\": \"2021-08-01T07:00:00.000000Z\"\n    }\n]\n```\n\nhttp://127.0.0.1:8000/api/posts?title=for\u0026status=0\n\n```json\n[]\n```\n\n### Testing\n\n```bash\ncomposer test\n```\n\n### Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](CONTRIBUTING.md) for details.\n\n### Security\n\nIf you discover any security related issues, please email caizhenghai@gmail.com instead of using the issue tracker.\n\n## Credits\n\n- [forecho](https://github.com/forecho)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n\n## Laravel Package Boilerplate\n\nThis package was generated using the [Laravel Package Boilerplate](https://laravelpackageboilerplate.com).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fforecho%2Flaravel-repository","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fforecho%2Flaravel-repository","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fforecho%2Flaravel-repository/lists"}