My Laravel resource organisation

When adding a new resource called Foo to a Laravel web app/site, I add the following files.

  • Controller - App\Http\Controllers
    • FooController
      • command: php artisan make:controller FooController --resource
  • Model - App\Models
    • Foo
      • command: php artisan make:model Foo
  • Request - App\Http\Requests\Foo
    • StoreFooRequest
  • Services - App\Services\Foo
    • FooCollectionFetcher
  • Resources - App\Http\Resources
    • FooResource
      • command: php artisan make:resource FooResource
    • FooCollectionResource
      • command: php artisan make:resource FooCollectionResource --collection

Notes

  • Everything above is technically optional. (Controller classes are optional if you're willing to do everything from the routes file. 😬) This setup gives me my preferred level of modularity.
  • I create a resource-specific directory when there may be > 2 files (eg. Services may contain other helper classes) or existing resources use that structure (as the case with Requests).
  • Always use the singular form for file and class names, eg. Foo never Foos. In the case of "lists/many" vs "single/one", I use the term "collection", eg. FooResource (process a single foo) and FooCollectionResource (process a list of foos).

Links to this note

  • Est ex deserunt esse ut pariatur quis fugiat id velit commodo

    Ut anim fugiat laboris et eiusmod aliquip.

  • Est ex deserunt esse ut pariatur quis fugiat id velit commodo

    Ut anim fugiat laboris et eiusmod aliquip.

  • Est ex deserunt esse ut pariatur quis fugiat id velit commodo

    Ut anim fugiat laboris et eiusmod aliquip.

  • Est ex deserunt esse ut pariatur quis fugiat id velit commodo

    Ut anim fugiat laboris et eiusmod aliquip.

  • Est ex deserunt esse ut pariatur quis fugiat id velit commodo

    Ut anim fugiat laboris et eiusmod aliquip.

  • Est ex deserunt esse ut pariatur quis fugiat id velit commodo

    Ut anim fugiat laboris et eiusmod aliquip.

  • Est ex deserunt esse ut pariatur quis fugiat id velit commodo

    Ut anim fugiat laboris et eiusmod aliquip.