{"id":15029200,"url":"https://github.com/jsdecena/baserepo","last_synced_at":"2025-04-07T06:07:04.403Z","repository":{"id":32728330,"uuid":"141069835","full_name":"jsdecena/baserepo","owner":"jsdecena","description":"Base repository","archived":false,"fork":false,"pushed_at":"2022-05-03T08:03:55.000Z","size":62,"stargazers_count":76,"open_issues_count":0,"forks_count":42,"subscribers_count":5,"default_branch":"master","last_synced_at":"2025-03-31T04:07:26.239Z","etag":null,"topics":["laravel","laravel-5-package","lumen","lumen-package","php","php7","repository","repository-pattern","tdd","test-driven-development"],"latest_commit_sha":null,"homepage":null,"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/jsdecena.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-07-16T00:58:51.000Z","updated_at":"2024-09-03T09:43:23.000Z","dependencies_parsed_at":"2022-08-08T06:00:15.140Z","dependency_job_id":null,"html_url":"https://github.com/jsdecena/baserepo","commit_stats":null,"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsdecena%2Fbaserepo","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsdecena%2Fbaserepo/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsdecena%2Fbaserepo/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jsdecena%2Fbaserepo/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jsdecena","download_url":"https://codeload.github.com/jsdecena/baserepo/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247601447,"owners_count":20964864,"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-5-package","lumen","lumen-package","php","php7","repository","repository-pattern","tdd","test-driven-development"],"created_at":"2024-09-24T20:09:56.892Z","updated_at":"2025-04-07T06:07:04.383Z","avatar_url":"https://github.com/jsdecena.png","language":"PHP","funding_links":["https://ko-fi.com/G2G0ADEK"],"categories":[],"sub_categories":[],"readme":"# Base Repository Package\n\n[![master](https://github.com/jsdecena/baserepo/actions/workflows/master.yaml/badge.svg)](https://github.com/jsdecena/baserepo/actions/workflows/master.yaml)\n[![Latest Stable Version](https://poser.pugx.org/jsdecena/baserepo/v/stable)](https://packagist.org/packages/jsdecena/baserepo)\n[![Total Downloads](https://poser.pugx.org/jsdecena/baserepo/downloads)](https://packagist.org/packages/jsdecena/baserepo)\n[![License](https://poser.pugx.org/jsdecena/baserepo/license)](https://packagist.org/packages/jsdecena/baserepo)\n[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fjsdecena%2Fbaserepo.svg?type=shield)](https://app.fossa.io/projects/git%2Bgithub.com%2Fjsdecena%2Fbaserepo?ref=badge_shield)\n\n## Sign-up with [Digital Ocean and get $20 discount](https://m.do.co/c/bce94237de96)!\n\n## Buy me a [coffeee](https://ko-fi.com/G2G0ADEK) so I can continue development of this package\n\n## How to install\n\n- Run in your terminal `composer require jsdecena/baserepo`\n\n- In your repository class, extend it so you can use the methods readily available.\n\n```php\n\nnamespace App\\Repositories;\n\nuse App\\User;\nuse Illuminate\\Http\\Request;\nuse Illuminate\\Database\\QueryException;\nuse Jsdecena\\Baserepo\\BaseRepository;\n\nclass UserRepository extends BaseRepository {\n    \n    public function __construct(User $user) \n    {\n        parent::__construct($user);\n    }\n    \n    public function createUser(array $data) : User\n    {\n        try {\n            return $this-\u003ecreate($data);\n        } catch (QueryException $e) {\n            throw new \\Exception($e);\n        }\n    }\n}\n```\n\n- Then, use it in your controller.\n\n```php\n\nuse App\\Http\\Controllers\\Controller;\nuse App\\Repositories\\UserRepository;\nuse App\\User;\n\nclass MyController extends Controller {\n    \n    private $userRepository;\n    \n    /**\n    *\n    * Inject your repository or the interface here\n    */\n    public function __construct(UserRepository $userRepository) \n    {\n        $this-\u003euserRepository = $userRepository;\n    }\n\n    public function index() \n    {\n        $user = $this-\u003euserRepository-\u003eall();\n\n        return response()-\u003ejson($user);    \n    }\n    \n    public function store(Request $request)\n    {\n        // do data validation\n    \n        try {\n            \n            $user = $this-\u003euserRepository-\u003ecreateUser($request-\u003eall());\n    \n            return response()-\u003ejson($user, 201);\n        \n        } catch (Illuminate\\Database\\QueryException $e) {\n            \n            return response()-\u003ejson([\n                'error' =\u003e 'user_cannot_create',\n                'message' =\u003e $e-\u003egetMessage()\n            ]);        \n        }\n    }\n\n    public function show($id)\n    {\n        // do data validation\n        \n        try {\n            \n            $user = $this-\u003euserRepository-\u003efindOneOrFail($id);\n    \n            return response()-\u003ejson($user);\n            \n        } catch (Illuminate\\Database\\Eloquent\\ModelNotFoundException $e) {\n            \n            return response()-\u003ejson([\n                'error' =\u003e 'user_no_found',\n                'message' =\u003e $e-\u003egetMessage()\n            ]);\n        }\n    }\n    \n    public function update(Request $request, $id)\n    {\n        // do data validation\n        \n        try {\n            \n            $user = $this-\u003euserRepository-\u003efindOneOrFail($id);\n           \n            // You can also do this now, so you would not have to instantiate again the repository\n            $this-\u003euserRepository-\u003eupdate($request-\u003eall(), $user);\n    \n            return response()-\u003ejson($user);\n            \n        } catch (Illuminate\\Database\\Eloquent\\ModelNotFoundException $e) {\n            \n            return response()-\u003ejson([\n                'error' =\u003e 'user_no_found',\n                'message' =\u003e $e-\u003egetMessage()\n            ]);            \n            \n        } catch (Illuminate\\Database\\QueryException $e) {\n            \n            return response()-\u003ejson([\n                'error' =\u003e 'user_cannot_update',\n                'message' =\u003e $e-\u003egetMessage()\n            ]);\n        }\n    }\n    \n    public function destroy($id)\n    {\n        // do data validation\n        \n        try {\n            \n            $user = $this-\u003euserRepository-\u003efindOneOrFail($id);\n            \n            // Create an instance of the repository again \n            // but now pass the user object. \n            // You can DI the repo to the controller if you do not want this.\n            $userRepo = new UserRepository($user);\n            $userRepo-\u003edelete()\n    \n            return response()-\u003ejson(['data' =\u003e 'User deleted.']);\n            \n        } catch (Illuminate\\Database\\Eloquent\\ModelNotFoundException $e) {\n            \n            return response()-\u003ejson([\n                'error' =\u003e 'user_no_found',\n                'message' =\u003e $e-\u003egetMessage()\n            ]);            \n            \n        } catch (Illuminate\\Database\\QueryException $e) {\n            \n            return response()-\u003ejson([\n                'error' =\u003e 'user_cannot_delete',\n                'message' =\u003e $e-\u003egetMessage()\n            ]);\n        }\n    }    \n    \n}\n```\n\n# Testing\n\n- Run `make test`\n\n# Author\n\n[Jeff Simons Decena](https://jsdecena.me)\n\n\n## License\n[![FOSSA Status](https://app.fossa.io/api/projects/git%2Bgithub.com%2Fjsdecena%2Fbaserepo.svg?type=large)](https://app.fossa.io/projects/git%2Bgithub.com%2Fjsdecena%2Fbaserepo?ref=badge_large)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjsdecena%2Fbaserepo","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjsdecena%2Fbaserepo","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjsdecena%2Fbaserepo/lists"}