News

Laravel API Resources Artisan Command

Published
Laravel API Resources Artisan Command image

The Laravel Resources package is an artisan command for speeding up the development of APIs by creating the boilerplate code around a default API structure:

You interface with this package via the resources:create command, which will generate all the files you’ll need to build an API resource:

php artisan resources:create Post
Checking if the model exists ...
The model Post does not exist.
 
Should I create it? (yes/no) [yes]:
>
Should I create the migration for Post? (yes/no) [yes]:
>
Should I create the factory for Post? (yes/no) [yes]:
>
Should I create the seeder for Post? (yes/no) [yes]:
>
 
Creating 6 resources ...

The command will create the following files for you:

  • Controller
  • Form request
  • Resource and resource collection
  • Policy
  • Model
  • Database factory
  • Database migration
  • Database seeder
  • Routes

Here’s the format of the routes added to the routes/api.php file:

/*
|--------------------------------------------------------------------------
| Post endpoints
|--------------------------------------------------------------------------
*/
Route::name('posts.')->prefix('posts')->group(function () {
Route::get('/', 'PostControllerAPI@index')->name('index');
Route::post('/', 'PostControllerAPI@store')->name('create');
Route::get('/{post}', 'PostControllerAPI@show')->name('show');
Route::patch('/{post}', 'PostControllerAPI@update')->name('update');
Route::delete('/{post}', 'PostControllerAPI@destroy')->name('destroy');
});

The package has a customizable configuration which affects things like file location(s), filename prefix and suffixes. You can learn more about this package by checking out the code and readme on GitHub: LaravelResources.

Paul Redmond photo

Staff writer at Laravel News. Full stack web developer and author.

Filed in

Sponsored

laravelcloud logo
Laravel Cloud

Easily create and manage your servers and deploy your Laravel applications in seconds.

Visit Laravel Cloud

The latest

View all →
PayZephyr: One Payment API for Stripe, Paystack, and PayPal image

PayZephyr: One Payment API for Stripe, Paystack, and PayPal

Read article
Bifrost Turns One With AI Builds and New Workflows image

Bifrost Turns One With AI Builds and New Workflows

Read article
Preview Blade Templates in macOS Finder with Quick Blade image

Preview Blade Templates in macOS Finder with Quick Blade

Read article
Artisan Debugging Commands in Laravel Telescope 5.24.0 image

Artisan Debugging Commands in Laravel Telescope 5.24.0

Read article
Queue totalSize() and JobInterrupted Event in Laravel 13.31 image

Queue totalSize() and JobInterrupted Event in Laravel 13.31

Read article
Find Unexpected Test Inputs with Fuzz for Pest image

Find Unexpected Test Inputs with Fuzz for Pest

Read article