You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel Scout报错:Class 'App\Order' not found 求助

Fixing "Class 'App\Order' not found" in Laravel Scout

Hey there, let's walk through the common reasons you're hitting this error and how to resolve them step by step:

1. Verify the Order Model Exists

First things first—double-check that you actually have an Order model file at app/Order.php. If it's missing, create it using Artisan (or manually):

php artisan make:model Order

Make sure the file has the correct namespace, class definition, and the Scout Searchable trait (required for Scout to work with the model):

namespace App;

use Illuminate\Database\Eloquent\Model;
use Laravel\Scout\Searchable;

class Order extends Model
{
    use Searchable;

    // Optional: Define which attributes should be indexed for search
    public function toSearchableArray()
    {
        return [
            'id' => $this->id,
            // Add other fields you want users to search through
        ];
    }
}

2. Fix Route File Imports

Your route closure references Request and App\Order, but PHP won't recognize these classes unless you import them (or use their full namespace). You have two simple fixes:

Option A: Add Imports to the Top of Your Route File

At the start of routes/web.php (or whichever route file you're using), add these lines:

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

Route::get('warbands/search', function (Request $request) {
    return Order::search($request->search)->get();
});

Option B: Use Fully Qualified Class Names

If you prefer not to add imports, prefix the classes with their full namespace directly in the closure:

Route::get('warbands/search', function (\Illuminate\Http\Request $request) {
    return \App\Order::search($request->search)->get();
});

3. Refresh Composer's Autoloader

If you just created the Order model, Composer's autoloader might not have picked up the new file yet. Run this command to update the autoload mapping:

composer dump-autoload

4. Double-Check for Typos

It sounds silly, but typos are a common culprit! Make sure:

  • The model filename is Order.php (not Orders.php or order.php—PHP is case-sensitive for class names on most systems)
  • The namespace in the model is exactly namespace App;
  • Your route code references App\Order (not a misspelled variant like App\Ordre)

Once you've worked through these checks, your search route should run without the "Class not found" error.

内容的提问来源于stack exchange,提问作者Evin Wijninga

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 03:43:00