Target class [App\Http\Controllers\OrderController] does not exist

Viewed 4467

I'm getting this error "Target class [App\Http\Controllers\OrderController] does not exist." when ever i run "php artisan route:list and I can't determine what or where is the problem in this code or I am just Dumb tho

<?php

namespace App\Http\Controllers;

use App\Order;
use Auth;
use Illuminate\Http\Request;

class OrderController extends Controller
{
    public function index()
    {
        return response()->json(Order::with(['product'])->get(),200);
    }

    public function deliverOrder(Order $order)
    {
        $order->is_delivered = true;
        $status = $order->save();

        return response()->json([
            'status'    => $status,
            'data'      => $order,
            'message'   => $status ? 'Order Delivered!' : 'Error Delivering Order'
        ]);
    }

    public function store(Request $request)
    {
        $order = Order::create([
            'product_id' => $request->product_id,
            'user_id' => Auth::id(),
            'quantity' => $request->quantity,
            'address' => $request->address
        ]);

        return response()->json([
            'status' => (bool) $order,
            'data'   => $order,
            'message' => $order ? 'Order Created!' : 'Error Creating Order'
        ]);
    }

    public function show(Order $order)
    {
        return response()->json($order,200);
    }

    public function update(Request $request, Order $order)
    {
        $status = $order->update(
            $request->only(['quantity'])
        );

        return response()->json([
            'status' => $status,
            'message' => $status ? 'Order Updated!' : 'Error Updating Order'
        ]);
    }

    public function destroy(Order $order)
    {
        $status = $order->delete();

        return response()->json([
            'status' => $status,
            'message' => $status ? 'Order Deleted!' : 'Error Deleting Order'
        ]);
    }
}

here is my code from my order controller please help me and Thank you in advance for helping me

6 Answers

run below command

  php artisan config:cache 

or

 composer dump-autoload

after you ensue no spelling problem between controller and route, try one of these possible solutions:

  1. run php artisan config:cache
  2. run composer dump-autoload
  3. check your web.php routes and change the forward / slash to backward \

check this link it will help

First check your directory your controller is in seperate folder in controler folder or it is in controller folder. If your Controller file is in new folder like Controller/YourFolder/Your file the you use in web.php route like below

Route::get('your url','YourFOlderName\COntrolername@methodname')->name('your route 
name')

I hope its work fine

check out the route. E.g. in my case I ve gotten similar error after setting the route in web.php . It had been written wrong:

 Route::get('/dispatch_notes/{order_id}','order\AddNotesController.php@dispatchNotes')
  ->name('dispatch_notes');

Had its name wrong copypasted from controllers folder. After suggested playings with cache clearings saw that and removed the .php off its name and got it worked.

In laravel 8

In RouteServiceProvider uncomment

protected $namespace='App\\Http\\Controllers';

I had the same error for me the problem was I did not import the OrderController in my web.php. so always check if the file you trying to access on the web.php is imported

Related