ysm / laravel-repository-pattern
A Laravel package implementing the Repository Design Pattern with Service Layer
1.0.0
2025-04-21 15:36 UTC
Requires
- php: ^8.0
- illuminate/support: ^8.0|^9.0|^10.0|^11.0|^12.0
README
A lightweight Laravel package implementing the Repository Design Pattern with a Service Layer for clean database operations, supporting Eloquent relationships, sorting, and optional soft deletes. Compatible with Laravel 8.0 to 12.0 and PHP 8.0+.
Installation
-
Install via Composer:
composer require ysm/laravel-repository-pattern
-
Publish configuration:
php artisan vendor:publish --tag=config
Configuration
Edit config/repository-pattern.php
to manually define model-to-repository bindings:
return [ 'bindings' => [ \App\Models\User::class => \App\Repositories\UserRepository::class, ], ];
Artisan Commands
-
Generate a repository:
php artisan ysm:repository ModelName [--soft-deletes]
-
Generate a service:
php artisan ysm:service ModelName [--soft-deletes]
-
Generate a controller:
php artisan ysm:controller ModelName [--type=api|web] [--dir=CustomDir] [--soft-deletes] [--with-routes] [--with-rs] --type=api|web: Specify controller type (default: api) --dir=CustomDir: Custom controller and route directory (e.g., Api\Admin\v1) --soft-deletes: Include soft delete methods --with-routes: Generate and bind route file --with-rs: Generate repository and service
Methods
RepositoryInterface / BaseRepository
getAll(array $relations = [], string $orderBy = 'created_at', string $orderDir = 'DESC')
- Get all records with optional relationships and sorting.
paginate(int $perPage = 15, array $relations = [], string $orderBy = 'created_at', string $orderDir = 'DESC')
- Get paginated records with optional relationships and sorting.
show($value, string $column = 'id')
- Get a record by value and column.
create(array $data)
- Create a new record.
update($id, array $data)
- Update a record by ID.
delete($id)
- Delete a record by ID (hard delete).
with(array $relations)
- Load relationships for the query.
attach($id, string $relation, array $relatedIds)
- Attach IDs to a many-to-many relationship.
detach($id, string $relation, array $relatedIds)
- Detach IDs from a many-to-many relationship.
sync($id, string $relation, array $relatedIds)
- Sync IDs for a many-to-many relationship.
whereHas(string $relation, Closure $callback)
- Query records based on a relationship.
BaseService
- Mirrors all
RepositoryInterface
methods, delegating to the repository. getRepository()
- Get the underlying repository.
InteractsRepoWithSoftDeletes (Repository Trait)
softDelete($id)
- Soft delete a record by ID.
permanentlyDelete($id)
- Permanently delete a soft-deleted record.
restore($id)
- Restore a soft-deleted record.
InteractsServiceWithSoftDeletes (Service Trait)
softDelete($id)
- Soft delete a record via repository.
permanentlyDelete($id)
- Permanently delete a soft-deleted record via repository.
restore($id)
- Restore a soft-deleted record via repository.
Notes
- Compatibility: Requires Laravel 8.0–12.0 and PHP 8.0+. Ensure your project meets these requirements.
- Bindings: Model-to-repository bindings must be manually added to
config/repository-pattern.php
. No default binding exists for unbound models; consider adding a default repository or exception. - Controller Directory: Use
--dir=CustomDir
to place controllers inApp\Http\Controllers\CustomDir
( e.g.,Api\Admin\v1
) and routes inroutes/CustomDir/
. - Route Generation: The
--with-routes
flag forysm:controller
generates a route file inroutes/api/
,routes/web/
, orroutes/CustomDir/
(e.g.,routes/Api/Admin/v1/users.php
) and binds it inRouteServiceProvider
. Outputs “🎉 Routes created successfully!”. - Repository and Service Generation: The
--with-rs
flag forysm:controller
generates corresponding repository and service files, respecting--soft-deletes
. - Controller Overwrite: The
ysm:controller
command overwrites existing controllers, displaying “🎉 Controller [Name] (type) overwritten successfully.” or “🎉 Controller [Name] (type) created successfully.” - Repository Overwrite: The
ysm:repository
command overwrites existing repositories, displaying “Repository [Name] overwritten successfully.” or “Repository [Name] created successfully.”
License
MIT