laravel-repository-service maintained by nauxa-labs
Description
A flexible Repository and Service pattern implementation for Laravel applications
Author
Last update
2025/12/11 19:13
(dev-main)
License
Downloads
0
Tags
Laravel Repository Service
A flexible Repository and Service pattern implementation for Laravel applications.
✨ Features
- 🎯 Flexible Service Pattern:
ServiceContractandBaseServiceare intentionally empty, allowing you to define methods with any signature - 📦 Standard Repository Pattern:
RepositoryContractandEloquentRepositoryprovide standard CRUD operations - 🚀 Artisan Commands: Generate repositories and services with
make:repositoryandmake:service - 🔍 Enhanced Query Methods:
findWhere(),findWhereIn(),paginate(),with(),firstOrCreate() - ⚡ Laravel Integration: Auto-discovery support via Service Provider
- ✅ Fully Tested: Comprehensive test suite with PHPUnit
Requirements
- PHP ^8.1
- Laravel ^10.0 or ^11.0
Installation
composer require nauxa-labs/laravel-repository-service
The package will be auto-discovered by Laravel. No additional configuration needed.
Quick Start
Generate a Repository
php artisan make:repository User
This creates:
app/Repositories/UserRepository.php(interface)app/Repositories/UserRepositoryImplement.php(implementation)
Generate a Service
php artisan make:service User
This creates:
app/Services/UserService.php(interface)app/Services/UserServiceImplement.php(implementation)
Usage
Creating a Service
<?php
namespace App\Services;
use Nauxa\RepositoryService\Contracts\ServiceContract;
interface UserService extends ServiceContract
{
// Define your own methods with any signature
public function create(UserDTO $dto, ?string $role = null): User;
public function findByEmail(string $email): ?User;
}
<?php
namespace App\Services;
use Nauxa\RepositoryService\Abstracts\BaseService;
class UserServiceImplement extends BaseService implements UserService
{
public function __construct(
protected UserRepository $userRepository
) {}
public function create(UserDTO $dto, ?string $role = null): User
{
// Your implementation
}
public function findByEmail(string $email): ?User
{
// Your implementation
}
}
Creating a Repository
<?php
namespace App\Repositories;
use Nauxa\RepositoryService\Contracts\RepositoryContract;
interface UserRepository extends RepositoryContract
{
// Add custom methods if needed
public function findByEmail(string $email): ?User;
}
<?php
namespace App\Repositories;
use App\Models\User;
use Nauxa\RepositoryService\Abstracts\EloquentRepository;
class UserRepositoryImplement extends EloquentRepository implements UserRepository
{
public function __construct(User $model)
{
$this->model = $model;
}
public function findByEmail(string $email): ?User
{
return $this->findWhere(['email' => $email])->first();
}
}
Available Methods
EloquentRepository
| Method | Description |
|---|---|
find($id) |
Find a record by ID |
findOrFail($id) |
Find a record by ID or throw exception |
findWhere(array $conditions) |
Find records matching conditions |
findWhereIn(string $column, array $values) |
Find records where column is in values |
all() |
Get all records |
paginate(int $perPage = 15) |
Paginate results |
create(array $attributes) |
Create a new record |
firstOrCreate(array $attributes, array $values = []) |
First or create pattern |
update($id, array $attributes) |
Update a record |
delete($id) |
Delete a record |
destroy(array $ids) |
Delete multiple records |
with(array|string $relations) |
Set eager loading relations |
Eager Loading Example
// Load users with their posts
$users = $this->userRepository->with(['posts', 'profile'])->all();
// Or chain with other methods
$users = $this->userRepository->with('posts')->paginate(10);
BaseService
Empty by design - define your own methods!
Configuration
Publish the config to customize generator paths:
php artisan vendor:publish --tag=repository-service-config
Customize paths in config/repository-service.php:
'paths' => [
'repositories' => 'Repositories', // or 'Domain/User/Repositories'
'services' => 'Services',
],
Auto-Binding
Enable automatic binding to skip manual registration:
// config/repository-service.php
'auto_binding' => [
'enabled' => true,
],
Now just inject and use - no AppServiceProvider binding needed!
Publishing Stubs
You can publish the stubs to customize the generated code:
php artisan vendor:publish --tag=repository-service-stubs
Testing
composer test
Contributing
Please see CONTRIBUTING.md for details.
Changelog
Please see CHANGELOG.md for a list of changes.
License
MIT License - see LICENSE file.