I have a resource route for category controller
// category controller routes
Route::resource('api/category', CategoryController::class);
categorycontrooller.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use App\Category;
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 App\Http\Controllers\CategoryController::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.
CodePudding user response:
You are getting the error because the Resource Controller seems to be incomplete.
You could either:
- Create the resource controller. (https://laravel.com/docs/9.x/controllers#resource-controllers)
- Route resource only for index. (https://laravel.com/docs/9.x/controllers#restful-partial-resource-routes)
To follow step 1:
Delete/Rename the existing file and run:
php artisan make:controller CategoryController --resource
To follow step 2:
Edit your route to look like:
Route::resource('api/category', CategoryController::class)->only([
'index'
]);
CodePudding user response:
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:
Route::resource('api/categories', CategoryController::class);