API Resources acts as a transformation layer that sits between our Eloquent models and the JSON responses that are actually returned by our API.

Sample commands for creating the resource class and resource collection using php artisan create a resource class $ php artisan make:resource UserResource Exit fullscreen mode create a resource collection using either $ php artisan make:resource Users --collection $ php artisan make:resource UserCollection

Next, let’s define the inverse relationship on the Book model: app/Book.php public function user() { return $this->belongsTo(User::class); }

Unlike when you use the resource() method, which will in addition to generating API specific routes, also generate create and edit routes, which aren’t needed when building an API.
Newsletter

Get the latest Laravel/PHP jobs, events and curated articles straight to your inbox, once a week

Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge. Glimpse streamlines Laravel development by seamlessly deploying GitHub pull requests to preview environments with the help of Laravel Forge.
Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Shirts painstakingly handcrafted by under-caffeinated developers. Shirts painstakingly handcrafted by under-caffeinated developers.
Community Partners