Search code examples
phplaravelroutescontrollercrud

How to fix Target class controller does not exist in Laravel? It works on other controller but failed in this one's particular controller


I got error message of Target class [KategoriController] does not exist. however, other controller such as buku and user are working fine. I don't know why this particular controller failed.

links/index into kategori pages code, both result are the same

<a class="btn btn-warning" href="/admin/kategori"> Index Kategori</a>
<a class="btn btn-warning" href="{{ route('admin.index') }}"> Index Kategori</a>

routes

Route::resource('/admin/kategori',KategoriController::class)->middleware('auth:admin');

KategoriController

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Providers\RouteServiceProvider;
use App\Models\Kategori;

class KategoriController extends Controller
{
    protected function index()
    {
        $kategori = Kategori::orderBy('id')->paginate(5);
        return view('admin.kategori.index', compact('kategori'));
    }
    public function create()
    {
        return view('admin.kategori.create');
    }
    protected function store(Request $request)
    {
        $request->validate([
            'nama' => 'required',
        ]);
        $input = $request->all();
        Kategori::create($input);
        return redirect()->route('kategori.index')->with('success','Kategori has been created successfully.');
    }
    public function edit(Kategori $kategori)
    {
        return view('admin.kategori.edit',compact('kategori'));
    }
    protected function update(Request $request, Kategori $kategori)
    {
        $request->validate([
            'nama' => 'required',
        ]);
        $kategori->fill($request->post())->save();
        return redirect()->route('kategori.index')->with('success','Kategori Has Been updated successfully');
    }
    protected function destroy(Kategori $kategori)
    {
        $kategori->delete();
        return redirect()->route('kategori.index')->with('success','Kategori has been deleted successfully');
    }
}

So what's the problem's here because only KategoriController have this problems, UserController, PinjamController, and BukuController working as intended. Anyone know why this issues came up and how to fix it?


Solution

  • Remember to allways import all classes that you use. In your case is to:

    use App\Http\Controllers\KategoriController;
    
    Route::resource('/admin/kategori', KategoriController::class)->middleware('auth:admin');
    

    PS: I use personally use this extension for VSC to import all my classes, it's very easy.