Download the PHP package goodmanluphondo/laravel-service-repository-pattern without Composer
On this page you can find all versions of the php package goodmanluphondo/laravel-service-repository-pattern. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download goodmanluphondo/laravel-service-repository-pattern
More information about goodmanluphondo/laravel-service-repository-pattern
Files in goodmanluphondo/laravel-service-repository-pattern
Package laravel-service-repository-pattern
Short Description Service-Repository pattern for your Laravel applications
License MIT
Informations about the package laravel-service-repository-pattern
Laravel Service-Repository Pattern
A Laravel package that helps you implement the Service-Repository pattern in your Laravel applications. This package provides base classes and Artisan commands to quickly scaffold repositories, interfaces, and services following clean architecture principles.
Note: This package enforces separation of concerns between data access and business logic. Use it to maintain clean, testable, and maintainable code architecture.
This package includes the following features:
- Scaffolding Commands: Generate services, repositories, and interfaces with a single command
- Base Classes: Pre-built base repository and service classes with common CRUD operations
- Clean Architecture: Enforces separation of concerns between data access and business logic
- Namespace Support: Supports sub-namespaces for better organization
- Automatic Binding: Automatically registers repository interfaces with their implementations
Installation
Require this package with composer:
Laravel uses Package Auto-Discovery, so doesn't require you to manually add the ServiceProvider.
Laravel without auto-discovery:
If you don't use auto-discovery, add the ServiceProvider to the providers list. For Laravel 11 or newer, add the ServiceProvider in bootstrap/providers.php
. For Laravel 10 or older, add the ServiceProvider in config/app.php
.
Publish the base files
After installation, publish the base files to your application:
This will publish:
app/Interfaces/BaseInterface.php
- Base interface for all repositoriesapp/Repositories/Repository.php
- Base repository implementationapp/Providers/RepositoryServiceProvider.php
- Service provider for binding interfaces
Register the Repository Service Provider
After publishing, add the RepositoryServiceProvider
(this is the published file that binds your repository interfaces) to your config/app.php
(for Laravel 10 and older) or bootstrap/providers.php
(for Laravel 11+):
Usage
Creating a Simple Service
Use the make:service
command to create a new service class:
This creates app/Services/UserService.php
.
Creating a Service with Repository Pattern
For services tied to a model, use the -R
(or --repository
) flag to scaffold the complete repository structure:
This command will:
- Create
app/Interfaces/PostRepositoryInterface.php
(orapp/Interfaces/Posts/PostRepositoryInterface.php
for sub-namespaced models) - Create
app/Repositories/PostRepository.php
(orapp/Repositories/Posts/PostRepository.php
for sub-namespaced models) - Create
app/Services/PostService.php
(orapp/Services/Posts/PostService.php
for sub-namespaced models) - Automatically bind the interface to the repository in
RepositoryServiceProvider
Important: The model must exist before running this command. The command supports both root models (
app/Models/User.php
) and sub-namespaced models (app/Models/Blog/Post.php
).
Using Sub-namespaces
For services not tied to a model that need sub-namespace organization:
This creates app/Services/Integrations/PaymentService.php
.
Generated Structure
When using the -R
flag, the generated files follow this structure:
Repository Interface
Repository Implementation
Service
Available Repository Methods
The base repository provides these methods out of the box:
Method | Description |
---|---|
query() |
Get a query builder instance |
find($id) |
Find a model by ID (throws exception if not found) |
findAll() |
Get all models |
create(array $data) |
Create a new model |
update($id, array $data) |
Update a model by ID |
delete($id) |
Delete a model by ID |
firstOrCreate(array $data) |
Get first matching model or create new one |
firstWhere(string $column, $value) |
Get first model matching condition |
where(string $column, $value) |
Add where condition to query |
orderBy(string $column, string $direction = 'asc') |
Add order by to query |
Example Usage in Controller
Requirements
- PHP 8.0 or higher
- Laravel 9.0 or higher
License
This package is open-sourced software licensed under the MIT license.
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
All versions of laravel-service-repository-pattern with dependencies
illuminate/support Version ^12.0
illuminate/database Version ^12.0
illuminate/console Version ^12.0