Search code examples
laravel-5model-view-controllerroutesresources

Laravel Route apiResource (difference between apiResource and resource in route)


I'm using apiResource in Route which is using (index, create, show, update, destroy) methods in exampleController. When I would like to use show method the route wont work. what shall I do? I think it is because of {fruits} but I do not how solve it?

Route::apiResource('/fruit/{fruits}/apples', 'exampleController');

My route in browser is:

localhost:8000/api/fruits/testFruitSlug/apples/testAppleSlug

difference between apiResource and resource in route: Route::apiResource() only creates routes for index, store, show, update and destroy while Route::resource() also adds a create and edit route which don't make sense in an API context.


Solution

  • To quickly generate an API resource controller that does not include the create or edit methods, use the --api switch when executing the make:controller command:

    php artisan make:controller API/PhotoController --api
    

    Try using the command line to generate your controller. It will save you stress. You can then do this in your route

    Route::apiResource('photos', 'PhotoController');