A flexible Repository and Service pattern implementation for Laravel applications.
- π― 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
- PHP ^8.1
- Laravel ^10.0 or ^11.0
composer require nauxa-labs/laravel-repository-serviceThe package will be auto-discovered by Laravel. No additional configuration needed.
php artisan make:repository UserThis creates:
app/Repositories/UserRepository.php(interface)app/Repositories/UserRepositoryImplement.php(implementation)
php artisan make:service UserThis creates:
app/Services/UserService.php(interface)app/Services/UserServiceImplement.php(implementation)
<?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
}
}<?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();
}
}| 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 |
// Load users with their posts
$users = $this->userRepository->with(['posts', 'profile'])->all();
// Or chain with other methods
$users = $this->userRepository->with('posts')->paginate(10);Empty by design - define your own methods!
Publish the config to customize generator paths:
php artisan vendor:publish --tag=repository-service-configCustomize paths in config/repository-service.php:
'paths' => [
'repositories' => 'Repositories', // or 'Domain/User/Repositories'
'services' => 'Services',
],Enable automatic binding to skip manual registration:
// config/repository-service.php
'auto_binding' => [
'enabled' => true,
],Now just inject and use - no AppServiceProvider binding needed!
You can publish the stubs to customize the generated code:
php artisan vendor:publish --tag=repository-service-stubscomposer testPlease see CONTRIBUTING.md for details.
Please see CHANGELOG.md for a list of changes.
MIT License - see LICENSE file.