rakib-587 / laravel-make-repository
Laravel Artisan command to generate repositories with optional model binding.
Package info
github.com/rakib-587/laravel-make-repository
pkg:composer/rakib-587/laravel-make-repository
Requires
- php: >=8.1
- laravel/framework: ^10.0|^11.0|^12.0|^13.0
README
A lightweight Laravel package that provides Artisan commands to generate Repository classes following a clean and extendable pattern. Supports optional model binding and integrated model creation.
Supported Laravel versions: 10, 11, 12, and 13. This package is Laravel-only and is not intended for non-Laravel projects.
🚨 Breaking Changes (Major Release)
If you are upgrading from a previous major version, review these changes:
getAll()has been removed from the base repository.- Use
all()instead. - The package is now Laravel-only (no non-Laravel support target).
- Base repository API now follows stricter typing.
Quick migration example:
// Before UserRepository::getAll(); // After UserRepository::all();
📦 Installation
Require the package via Composer:
composer require rakib-587/laravel-make-repository
If auto-discovery doesn't work, manually register the service provider in config/app.php:
'providers' => [ Rakib\MakeRepository\MakeRepositoryServiceProvider::class, ],
⚙️ Usage
➤ Create a Repository for an Existing Model
php artisan make:repository User
This will generate:
app/Repositories/UserRepository.php- Uses
App\Models\Useras the associated model (automatically inferred)
➤ Create a Repository with an Explicit Model
php artisan make:repository Customer --model=User
This allows you to manually specify the model class to be used inside the repository.
➤ Create a Model with a Repository in One Command
php artisan make:model Product --repo
This command:
- Creates
app/Models/Product.php - Creates
app/Repositories/ProductRepository.php - Links the repository to the model automatically
Nested models are also supported:
php artisan make:model Admin/Test --repo
This generates:
app/Models/Admin/Test.phpapp/Repositories/Admin/TestRepository.php
🧱 Base Repository API
Generated repositories extend a typed base class.
namespace App\Repositories; use App\Models\User; use Rakib\MakeRepository\Repository; /** * @extends Repository<User> */ class UserRepository extends Repository { public static function model(): string { return User::class; } }
Available base methods:
query(): Builderall(array|string $columns = ['*']): Collectionfirst(array|string $columns = ['*']): ?ModelfirstOrFail(array|string $columns = ['*']): Modelfind(int|string $id, array|string $columns = ['*']): ?ModelfindOrFail(int|string $id, array|string $columns = ['*']): ModelfindMany(array $ids, array|string $columns = ['*']): Collectioncreate(array $attributes): Modelupdate(Model $model, array $attributes): booldelete(Model $model): bool|null(delete by model instance)destroy(int|string|array $ids): int(delete by id or id list)
Note: mass assignment rules still apply to
create()andupdate(). Ensure your model has the proper$fillableor$guardedconfiguration.
🧩 Customizing the Stub
The repository class is generated using a stub file that defines its structure and placeholders.
By default, the command looks for a local stub file in your Laravel project:
stubs/repository.stub
If the file does not exist, it will gracefully fall back to using the default stub provided by the package.
✏️ How to Customize
To customize the repository structure, first publish the stub file into your project:
php artisan vendor:publish --tag=make-repository-stubs
This will copy the stub to:
stubs/repository.stub
You can now edit the stub to change the generated structure.
Available placeholders:
{{ Namespace }}— The repository namespace (e.g.,App\Repositories\Admin){{ ClassName }}— The repository class name only (e.g.,UserRepository){{ ModelClass }}— The fully qualified model class used inusestatements (e.g.,App\Models\Admin\User){{ ModelName }}— The model short class name used in phpdoc and return type reference (e.g.,User)
Once the stub exists in your project, it will automatically be used for all future repository generations.
📁 Output Structure Example
When you run:
php artisan make:repository Order
You'll get:
// app/Repositories/OrderRepository.php namespace App\Repositories; use App\Models\Order; use Rakib\MakeRepository\Repository; class OrderRepository extends Repository { public static function model(): string { return Order::class; } }
📄 License
This package is open-sourced software licensed under the MIT license.
🙌 Author
Md Rakibul Islam GitHub: @rakib-587