I have a resource route for category controller
// category controller routes
Route::resource('api/category', CategoryController::class);
categorycontrooller.php
<?php
namespace AppHttpControllers;
use IlluminateHttpRequest;
use IlluminateHttpResponse;
use AppCategory;
class CategoryController extends Controller
{
public function pruebas()
{
return "controlador de categoria";
}
public function index()
{
$categories = Category::all();
return response()->json([
'code' => 200,
'status' =>'success',
'categories' => $categories
]);
}
}
php artisan route:list
GET|HEAD api/category ..... category.index › CategoryController@index
testing it with postman I got a 404 not found
BadMethodCallException: Method AppHttpControllersCategoryController::show does not exist. in file /var/www/html/api-rest-laravel/vendor/laravel/framework/src/Illuminate/Routing/Controller.php on line 68
I dont know what is hapenning.
2
Answers
You are getting the error because the Resource Controller seems to be incomplete.
You could either:
To follow step 1:
Delete/Rename the existing file and run:
To follow step 2:
Edit your route to look like:
It’s probably because you are trying to access the single category and that route uses
show
method in the controller which is not defined. Also it’s better to use plural name for your resource routes: