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

Fathom Analytics | Fast, simple and privacy-focused website analytics. Fathom Analytics | Fast, simple and privacy-focused website analytics.
Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future. Achieve superior email deliverability with ToastMail! Our AI-driven tool warms up inboxes, monitors reputation, and ensures emails reach their intended destination. Sign up today for a spam-free future.
Community Partners