l0n3ly / laravel-repository-with-service
Repository and service pattern package for Laravel
Package info
github.com/idehen-divine/laravel-repository-with-service
pkg:composer/l0n3ly/laravel-repository-with-service
Requires
- php: ^8.2
- illuminate/contracts: ^11.0||^12.0||^13.0
- spatie/laravel-package-tools: ^1.16
Requires (Dev)
- larastan/larastan: ^2.9||^3.0
- laravel/pint: ^1.14
- nunomaduro/collision: ^8.1.1||^7.10.0
- orchestra/testbench: ^11.0
- phpstan/extension-installer: ^1.3||^2.0
- phpstan/phpstan-deprecation-rules: ^1.1||^2.0
- phpstan/phpstan-phpunit: ^1.3||^2.0
- phpunit/phpunit: ^10.0|^11.0
- spatie/laravel-ray: ^1.35
README
A Laravel package that scaffolds the repository and service pattern โ generates repository and service classes with interfaces, and automatically binds them to their implementations via the container.
โจ Features
- ๐๏ธ Artisan Scaffolding - Generate repositories and services with
make:repositoryandmake:service - ๐ Auto Binding - Automatically binds interfaces to implementations via the service container
- ๐ Subdirectory Support - Organize classes in nested directories (e.g.,
Admin/UserRepository) - ๐ Paired Generation - Generate a repository and service together with a single command
- ๐ Interface-First - Always generates a contract interface alongside each implementation
- ๐จ Multiple Templates - Choose between API service template or blank template
- ๐ค Boost Compatible - Full support for Laravel Boost AI-powered workflows and Copilot skills
๐ Requirements
- PHP ^8.2
- Laravel 11, 12, or 13
๐ฆ Installation
Install the latest 1.x release:
composer require l0n3ly/laravel-repository-with-service
Publish the config file:
php artisan vendor:publish --tag=repository-with-service-config
Quick Start
Fastest Start: Generate Everything at Once
Scaffold a complete feature with one command:
php artisan make:model User --all
This creates everything โ model, migration, factory, seeder, controller, repository, and service.
Generate a Repository
# Basic repository php artisan make:repository User # Repository with paired service php artisan make:repository User --service # Repository with API-style service php artisan make:repository User --service --api # Repository in subdirectory php artisan make:repository Admin/User --service
Generate a Service
# Basic service php artisan make:service UserService # API service with response helpers php artisan make:service UserService --api # Service with paired repository php artisan make:service UserService --repository # Minimal service php artisan make:service UserService --blank
Generate from a Model
# Model with service and repository php artisan make:model Product --service --repository # Model with everything (fastest) php artisan make:model Order --all
Use in Your Code
Services and repositories are automatically bound to the container:
class UserController extends Controller { public function __construct( protected UserRepository $repository, protected UserService $service ) {} public function index() { return $this->service->getActiveUsers(); } }
๐ Documentation
Comprehensive documentation is available:
| Document | Purpose |
|---|---|
| Full Guide | Installation, quick start, and all commands |
| API Reference | Core interfaces and configuration |
| Troubleshooting | Common issues and solutions |
| Examples | Real-world usage examples |
| Contributing | Contribution guidelines |
| Laravel Boost Integration | Using with AI-powered Boost workflows |
๐ค Laravel Boost Support
This package is fully integrated with Laravel Boost and provides three AI skills to enhance your development workflow:
Three Essential Skills
- Repository Generator - Generate repositories with best practices
- Service Generator - Generate services with business logic
- Service Binding - Understand dependency injection and binding patterns
These skills are located in resources/boost/skills/ and are discovered by running:
php artisan boost:install --discover
Select the desired skills during installation, and they'll be available to your AI agent.
AI-Powered Workflows
Get intelligent suggestions for:
- Generating complete feature modules
- Creating repository hierarchies
- Setting up service structures
- Testing patterns and mocking
Example Boost Usage
User: "Generate a complete blog module with posts, comments, and categories"
Boost activates the skills and suggests:
โ PostRepository with PostService (API template)
โ CommentRepository with CommentService
โ CategoryRepository with CategoryService
โ BlogManager service orchestrating operations
Getting Started with Boost
- Install Laravel Boost in your Laravel project
- Run
php artisan boost:install --discover - Select this package's skills when prompted
- Ask your AI agent to generate repositories, services, or help with architecture
See BOOST.md for detailed integration guide.
๐ฏ Common Patterns
Blog Application
php artisan make:repository Post --service --api php artisan make:repository Category --service php artisan make:repository Post/Comment --service
E-Commerce
php artisan make:repository Product --service --api php artisan make:repository Order --service --api php artisan make:service Shop/Checkout --api php artisan make:service Shop/Inventory --api
SaaS with Multi-Tenancy
php artisan make:repository Tenant --service --api php artisan make:repository User --service --api php artisan make:repository Subscription --service --api
See EXAMPLES.md for complete real-world examples.
๐ง Configuration
Edit config/service-repository.php to customize:
return [ 'repository_directory' => 'app/Repositories', 'repository_namespace' => 'App\Repositories', 'service_directory' => 'app/Services', 'service_namespace' => 'App\Services', // ... naming conventions ];
Override Binding Example
Decorate or replace a bound implementation in your service provider:
$this->app->extend(UserRepository::class, function ($service, $app) { return new CachedUserRepository($service); });
Service API Template
The --api flag generates services with response helpers:
class UserServiceImplement implements UserService { use ResultService; public function create(array $data): array { try { $user = $this->repository->create($data); return $this->success($user, 'User created'); } catch (Exception $e) { return $this->error($e->getMessage()); } } }
๐ Performance Tips
- Use Caching - Wrap repositories with caching decorators
- Eager Loading - Use Eloquent's
with()in repositories - Singleton Services - For stateless services, bind as singleton
- Optimize Queries - Focus repository methods on specific queries
๐งช Testing
Mock repositories in tests:
public function test_service_logic() { $repository = Mockery::mock(UserRepository::class); $repository->shouldReceive('find')->with(1)->andReturn(['id' => 1]); $this->app->bind(UserRepository::class, fn() => $repository); $service = app(UserService::class); $result = $service->getUser(1); $this->assertEquals(1, $result['id']); }
๐ Real-World Examples
Check EXAMPLES.md for complete implementations:
- Blog with posts, comments, and categories
- E-commerce checkout process
- Multi-tenant SaaS structure
- Admin dashboards
- Testing patterns
๐ค Contributing
Contributions are welcome! See CONTRIBUTING.md for guidelines.
๐ License
The MIT License (MIT). Please see LICENSE.md for more information.
Support
- ๐ Documentation: See
docs/directory - ๐ Issues: Report on GitHub
- ๐ฌ Discussions: Ask questions on GitHub Discussions
- ๐ง Help: Check Troubleshooting Guide
Project guide:
Changelog
See CHANGELOG for release notes.
License
MIT. See LICENSE.md.