The target class does not exist. After renaming controller and model
P粉691958181
P粉691958181 2024-01-16 17:26:39
0
1
360

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粉691958181
P粉691958181

reply all(1)
P粉545956597

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);
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!