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.
A simple Laravel Artisan command to scaffold the Repository pattern: generates an Interface in app/Interfaces, a concrete Repository in app/Repositories, and auto-binds them in AppServiceProvider.php. Supports nested namespaces (e.g., Api\Admin\User).
app/Interfaces/Api/Admin/UserInterface.php).--force flag.You can install the package via Composer:
composer require jeishanul/laravel-repository-generator
The package will auto-discover its service provider. If not, add it manually to config/app.php under providers:
'providers' => [
// ...
Jeishanul\RepositoryGenerator\RepositoryGeneratorServiceProvider::class,
],
Run the command to generate files for a given repository:
php artisan make:repository User
Or with nested namespaces:
php artisan make:repository Api\Admin\User
--force: Overwrite existing files.For php artisan make:repository Api\Admin\User:
Interface: app/Interfaces/Api/Admin/UserInterface.php
<?php
namespace App\Interfaces\Api\Admin;
interface UserInterface
{
/**
* Get all users.
*/
public function all();
/**
* Find a user by ID.
*/
public function find($id);
/**
* Create a new user.
*/
public function create(array $data);
/**
* Update a user.
*/
public function update($id, array $data);
/**
* Delete a user.
*/
public function delete($id);
}
Repository: app/Repositories/Api/Admin/UserRepository.php
<?php
namespace App\Repositories\Api\Admin;
use App\Interfaces\Api\Admin\UserInterface;
use App\Models\User;
use Illuminate\Database\Eloquent\Collection;
class UserRepository implements UserInterface
{
protected $model;
public function __construct(User $model)
{
$this->model = $model;
}
public function all(): Collection
{
return $this->model->all();
}
public function find($id)
{
return $this->model->findOrFail($id);
}
public function create(array $data): User
{
return $this->model->create($data);
}
public function update($id, array $data): bool
{
$model = $this->find($id);
return $model->update($data);
}
public function delete($id): bool
{
$model = $this->find($id);
return $model->delete();
}
}
Auto-Binding: Appended to app/Providers/AppServiceProvider.php in the register() method:
public function register()
{
// ... existing code ...
$this->app->bind(App\Interfaces\Api\Admin\UserInterface::class, App\Repositories\Api\Admin\UserRepository::class);
}
Inject the interface for loose coupling:
<?php
namespace App\Http\Controllers;
use App\Interfaces\Api\Admin\UserInterface;
class UserController extends Controller
{
protected $userRepo;
public function __construct(UserInterface $userRepo)
{
$this->userRepo = $userRepo;
}
public function index()
{
return $this->userRepo->all();
}
public function store(Request $request)
{
return $this->userRepo->create($request->validated());
}
// ... other methods
}
src/Stubs/ in your fork.App\Models\User). For nested models, update the stub's import.AppServiceProvider for simplicity. For production, consider a dedicated RepositoryServiceProvider.composer test
(Tests use PHPUnit; add your own for custom stubs.)
Please see CHANGELOG for more information on what has changed recently.
Thank you for considering contributing! Please follow these steps:
git checkout -b feature/new-feature).git commit -am 'Add new feature').git push origin feature/new-feature).If you discover a security vulnerability, please email shishirjeishanul@gmail.com instead of opening an issue.
The MIT License (MIT). Please see License File for more information.
⭐ Star this repo if it helps your project! Follow @jeishanul on GitHub for more packages.
How can I help you explore Laravel packages today?