skip to Main Content

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


  1. You are getting the error because the Resource Controller seems to be incomplete.

    You could either:

    1. Create the resource controller. (https://laravel.com/docs/9.x/controllers#resource-controllers)
    2. 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'
    ]);
    
    Login or Signup to reply.
  2. 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);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search