jeishanul/laravel-repository-generator
Artisan generator for Laravel’s Repository pattern. Creates CRUD-ready interface and repository classes (supports nested namespaces), writes them to app/Interfaces and app/Repositories, and auto-binds them in AppServiceProvider. Laravel 10–12, --force overwrite.
Installation:
composer require jeishanul/laravel-repository-generator
No additional configuration is needed—auto-discovery handles the rest.
First Use Case:
Generate a repository for a User model in the default namespace:
php artisan make:repository User
This creates:
app/Interfaces/UserInterface.php (CRUD-ready interface)app/Repositories/UserRepository.php (concrete implementation)AppServiceProvider.Where to Look First:
app/Interfaces/ and app/Repositories/ for structure.AppServiceProvider.php (auto-generated).php artisan to see the full command syntax.Repository Generation:
php artisan make:repository [name] [--force] [--namespace=Namespace]
Api/Admin):
php artisan make:repository User --namespace=Api/Admin
Generates:
app/Interfaces/Api/Admin/UserInterface.phpapp/Repositories/Api/Admin/UserRepository.phpIntegration with Controllers: Inject the interface into controllers (auto-resolved via IoC):
use App\Interfaces\UserInterface;
class UserController extends Controller {
public function __construct(private UserInterface $userRepository) {}
}
Extending Interfaces: Add custom methods to the interface and implement them in the repository:
// app/Interfaces/UserInterface.php
public function getActiveUsers(): Collection;
// app/Repositories/UserRepository.php
public function getActiveUsers(): Collection {
return User::active()->get();
}
Api/Admin for admin APIs).UserInterface for unit tests).Overwrite Protection:
--force to bypass:
php artisan make:repository User --force
Namespace Collisions:
User in both root and Api/Admin). Use unique names:
php artisan make:repository AdminUser --namespace=Api/Admin
Binding Conflicts:
AppServiceProvider, ensure they don’t clash with auto-generated bindings. Delete old bindings if regenerating.Missing Bindings:
config/app.php).php artisan config:clear if bindings appear missing.File Permissions:
app/Interfaces/ and app/Repositories/. Set permissions if needed:
chmod -R 755 app
Custom Templates:
php artisan vendor:publish --tag=repository-generator-stubs
resources/stubs/repository.stub and resources/stubs/interface.stub.Additional Methods:
Laravel Version Quirks:
trait UsesUserRepository {
public function __construct(private UserInterface $userRepo) {}
}
spatie/laravel-query-builder for advanced query methods in repositories.# .github/workflows/laravel.yml
- run: php artisan make:repository User --force
How can I help you explore Laravel packages today?