Svetlozar
Svetlozar

Reputation: 987

Laravel apiResources how to call specific method

I am trying to use Laravel apiResource but obviously there is something that I can't understand of the usage. In a simpe route you have something like that

    Route::get('user/{id}', 'UserController@show');

where show is the method you want to call.

How can I specify which method to be called in apiResource?

Currently I have

  Route::apiResources(['user' =>'API\UserController']);

which call the store method in the UserController. I would like to specify another method to be called but for example

 Route::apiResources(['user' =>'API\UserController@show']);

is not going to call the show method

So how can I specify which method to be called in apiResources?

Upvotes: 2

Views: 2413

Answers (1)

Rehmat
Rehmat

Reputation: 5071

For a resource, show, store, update, index methods are reserved. Here is how it is going to work:

  • A GET call on the route without any ID will call the index() method
  • A GET call on the route with a resource ID will call the show() method
  • A POST call on the route will call the store() method
  • A PATCH call on a route with ID will call the update() method

The API resource route is identical to the web resource route except it doesn't come with the methods that return the views, i.e. create(), edit() etc.

If you need any custom routes, you need to define the needed routes before defining the API resource route. I hope this helps. More info is present in the official docs here.

Upvotes: 6

Related Questions