Laravel Collections and Resources

RMAG news

When working with API’s I’d been becoming frustrated with the process around creating resources and collections. Sure when creating resources which seemed to follow a different pattern as when creating migrations, controllers, requests and what not.

Previously when creating contact resource I had used:

a make:resource ContactResource

With a being an alias for PHP artisan.

This was fine for the resource but I also needed a collection.

Intuitively I tried:

a make:collection ContactCollection

Which was the wrong way to go about this.

a make:resource –collection ContactColllection

In the given scenario, Laravel knows that it should create both the resource and its associated collection. This distinction ensures that Laravel sets up the necessary files and configurations for both the resource and its collection correctly.

Leave a Reply

Your email address will not be published. Required fields are marked *