Home  >  Q&A  >  body text

The target class does not exist. After renaming controller and model

When I start this project in Laravel, I have a ReturnController. But due to naming convention, I had to rename it to OrderController. I renamed the controller and model, I ran all php artisan cache:clear, php artisan route:cache, php artisan config:cache ... etc. but when I try to create in When ordering>views\orders\add.blade.php (The action of the form is action="{{route('orders.store') }}")

I get the errorThe target class [OrderController] does not exist.

This is web.phpContent:

Route::get('/', function () {
    return view('welcome');
});

Route::resource('customers', 'CustomerController');

Route::resource('orders', 'OrderController');

Auth::routes();

Route::get('/home', [App\Http\Controllers\OrderController::class, 'showOrders'])->name('orders');

Route::get('/orders/create', [App\Http\Controllers\OrderController::class, 'create'])->name('orders.create');

Route::get('/orders', [App\Http\Controllers\OrderController::class, 'index']);

What did I miss? How can I fix this before rewriting the entire application from scratch with the correct controller names?

Another clue is that in order to navigate to http://127.0.0.1:8000/home, strangely I have to add the line use App\Models\Order in the OrderController .php in order to work...so here is OrderController.php to help...

<?php

namespace App\Http\Controllers;
use App\Models\Order;
use App\Models\Customer;
use App\Models\Product;

use Illuminate\Http\Request;

class OrderController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     */
    public function create()
    {
        $customers = Customer::all();
        $products = Product::all();
        return view('orders.add', compact('customers', 'products'));
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     */
    public function show(string $id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     */
    public function edit(string $id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     */
    public function update(Request $request, string $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     */
    public function destroy(string $id)
    {
        //
    }

    public function showOrders()
    {
        $orders = Order::with(['customer', 'products'])->get();

        return view('home', compact('orders'));
    }
}

P粉691958181P粉691958181251 days ago356

reply all(1)I'll reply

  • P粉545956597

    P粉5459565972024-01-17 14:58:06

    In web.php instead of:

    Route::resource('customers', 'CustomerController');
    
    Route::resource('orders', 'OrderController');

    use:

    Route::resource('customers', App\Http\Controllers\CustomerController::class);
    Route::resource('orders', App\Http\Controllers\OrderController::class);

    reply
    0
  • Cancelreply