This Generator package provides generators of Models, Controllers, Request, Routes & Tests for a painless development.
Install the package through Composer.
Run the Composer require command from the Terminal:
composer require andreacivita/api-crud-generator --dev
Run this command from the Terminal
php artisan vendor:publish
Select andreacivita/api-crud-generator and setup it's complete.
Usage of this package is very simple.
First, let's supposing I want to generate CRUD for all table in my db.
So, we run
php artisan make:crud --all
No further options required. Your setup is complete!
You can manage a single table with interactive mode or manually (see next paragraph).
Just run
php artisan make:crud --interactive
Crud generator will ask you several data (Name of resource, Table name and use of timestamps).
Now i suppose generation of CRUD operations of Car db table.
Run this command:
php artisan make:crud Car
Done! You will have Car model (located in App/Model directory), CarController, CarRequest (used for input data) and Routes (located in routes/api.php).
By default, DB Table's name is plural, while Model class name is singular (e.g. Table => Cars, Model => Car). You can change this behavior specifying the name in terminal
php artisan make:crud Car --table Car
This will create the same resources, but table name in model will be 'Car' (instead of default 'Cars')
By default, this packages will set all timestamps to false. You can change this doing this command:
php artisan make:crud Car --timestamps true
This will set 'timestamps=true' in Model class.
By default, Routes will be not protected by passport. However, you can generate Passport-protected routes with:
php artisan make:crud Car --passport
This will set 'timestamps=true' in Model class.
Routes will follow Route::resource() Schema (default routing schema provided by Laravel).
Example: i'm generating Car crud
Route | Method | Operation |
---|---|---|
car | GET | Get all cars |
car/{id} | GET | Find car by id |
car | POST | Insert a new car |
car/{id} | PUT / PATCH | Update car by id |
car/{id} | DELETE | Delete car by id |
Remember that all api routes have 'api/' prefix.
When created CRUD structure (Controllers, Models, Request, Resource, Factory & Routes), this package generate Feature test file.
Tests now require factory class to manipulate data. You should provide data schema into your factory class (using Faker), so you'll be able to test easily your API
Write better code will helps you so much! So, default behavior of Controllers is to force you to use validated data. You have to set all validation rules into your FormRequest class.
E.g. For Car Crud, you will set rules into your App\Request\CarRequest.php
This package is covered by MIT license. You are able to do whatever you want with this code.
Please feel free to fork this package and contribute by submitting a pull request to enhance the functionalities. You can see issues or enhancement and assign task for contributing :)
Star this repo or follow me on GitHub. And, if you want, you can share this link! :)
This package has been originally developed by Andrea Civita
A special thanks goes to Bastianjoel for it's pull request