https://github.com/forecho/laravel-repository
A generic repository implementation for Laravel with a few extras.
https://github.com/forecho/laravel-repository
laravel laravel-package repository
Last synced: over 1 year ago
JSON representation
A generic repository implementation for Laravel with a few extras.
- Host: GitHub
- URL: https://github.com/forecho/laravel-repository
- Owner: forecho
- License: mit
- Created: 2022-09-02T06:07:23.000Z (almost 4 years ago)
- Default Branch: main
- Last Pushed: 2024-03-02T14:27:13.000Z (over 2 years ago)
- Last Synced: 2025-03-18T14:53:34.424Z (over 1 year ago)
- Topics: laravel, laravel-package, repository
- Language: PHP
- Homepage:
- Size: 28.3 KB
- Stars: 5
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: CONTRIBUTING.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Laravel Repository
[](https://packagist.org/packages/forecho/laravel-repository)
[](https://packagist.org/packages/forecho/laravel-repository)

The Laravel Repository package is meant to be a generic repository implementation for Laravel.
## Installation
You can install the package via composer:
```bash
composer require forecho/laravel-repository
```
## Usage
Create a repository class:
```php
'desc'];
// Optional
public array $booleanAttributes = ['status'];
public string $modelClass = Post::class;
// Optional
public function boot()
{
$this->pushCriteria(UserCriteriaCriteria::class);
}
}
```
Use the repository in your controller:
```php
repository = $accountRepository;
}
public function index(Request $request, PostRepository $userRepository)
{
$posts = $this->repository->search($request->all())->paginate();
// $posts = $this->repository->with(['user'])->search($request->all())->paginate();
return response()->json(UserResource::collection($posts);
}
}
```
## Criteria
Create a criteria class:
```php
where('user_id', UserService::getUserId());
}
}
```
Request all data without filter by request:
http://127.0.0.1:8000/api/posts
```json
[
{
"id": 1,
"title": "title",
"content": "content",
"status": 0,
"created_at": "2021-08-01T07:00:00.000000Z",
"updated_at": "2021-08-01T07:00:00.000000Z"
},
{
"id": 2,
"title": "for echo",
"content": "content",
"status": 1,
"created_at": "2021-08-01T07:00:00.000000Z",
"updated_at": "2021-08-01T07:00:00.000000Z"
}
]
```
http://127.0.0.1:8000/api/posts?title=for
```json
[
{
"id": 2,
"title": "for echo",
"content": "content",
"status": 1,
"created_at": "2021-08-01T07:00:00.000000Z",
"updated_at": "2021-08-01T07:00:00.000000Z"
}
]
```
http://127.0.0.1:8000/api/posts?status=0
```json
[
{
"id": 1,
"title": "title",
"content": "content",
"status": 0,
"created_at": "2021-08-01T07:00:00.000000Z",
"updated_at": "2021-08-01T07:00:00.000000Z"
}
]
```
http://127.0.0.1:8000/api/posts?title=for&status=0
```json
[]
```
### Testing
```bash
composer test
```
### Changelog
Please see [CHANGELOG](CHANGELOG.md) for more information what has changed recently.
## Contributing
Please see [CONTRIBUTING](CONTRIBUTING.md) for details.
### Security
If you discover any security related issues, please email caizhenghai@gmail.com instead of using the issue tracker.
## Credits
- [forecho](https://github.com/forecho)
- [All Contributors](../../contributors)
## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.
## Laravel Package Boilerplate
This package was generated using the [Laravel Package Boilerplate](https://laravelpackageboilerplate.com).