Laravel Scout报错:Class 'App\Order' not found 求助
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(notOrders.phpororder.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 likeApp\Ordre)
Once you've worked through these checks, your search route should run without the "Class not found" error.
内容的提问来源于stack exchange,提问作者Evin Wijninga

