Create Repositories In Laravel With An Artisan Command

Hi there dev!

In this brief article, I will be sharing a useful resource you could use in your Laravel projects while building your APIs with the repository pattern.

From my perspective, It's a common, a little bit of frustration among laravel developers who use the repository pattern regularly, to have to manually create repository class and interfaces.

Hence, the solution!

I wrote a small package called laravel-api-interface-gen. which gives you ability to use the following artisan commands.

php artisan make:repositoryinterface Book 
php artisan make:repository Book
php artisan make:interface Book

The command php artisan make:repositoryinterface Book will create a Repositories folder in your project and within the Repositories folder, It will create the Interfaces folder. Then, it creates the repository and Interface (in this case) called BookRepository and BookRepositoryInterface in their respective folders. Then, it also creates the RepositoryServiceProvider file in your Providers folder where you bind the repository to the interface created. After which you can register the RepositoryServiceProvider in your app config file.

You also will be able to simply generate a repository (without the interface) using,

php artisan make:repository ModelName

Likewise, generate an interface (without the repository) using,

php artisan make:interface ModelName

To install, from your console, run the command,

composer require idtitanium/laravel-api-interface-gen

Supports Laravel 6 and above.

If you want to know more about about the Repository Pattern in laravel, I have a beginner article HERE.

Happy Coding :)