Laravel 8数据库填充报错:类未找到与Trait缺失问题求助
Hey there! Let's work through those two errors you're hitting with your Laravel 8 database seeding. I'll break down each issue and show you how to fix them:
1. Fixing "Class 'Database\Seeders\App\Models\Admin' not found"
The root cause here is the namespace context in your AdminsTableSeeder. Since the seeder lives under the Database\Seeders namespace, when you write App\Models\Admin::create(), PHP tries to look for the class at Database\Seeders\App\Models\Admin—which doesn't exist.
You have two solid fixes for this:
- Option 1: Import the Admin class at the top of your seeder
Add ausestatement for the Admin model so you can reference it directly:<?php namespace Database\Seeders; use Illuminate\Database\Seeder; use DB; use App\Models\Admin; // Add this line class AdminsTableSeeder extends Seeder { public function run() { DB::table('admins')->delete(); $adminRecords = [ [ 'id'=>1, 'name'=>'admin', 'type'=>'admin', 'email'=>'admin@admin.com', 'password'=>'', 'image'=>'', 'status'=>1, ], ]; foreach ($adminRecords as $key => $record){ Admin::create($record); // Now this works! } } } - Option 2: Use the root namespace prefix
If you prefer not to add theusestatement, prefix the class path with a backslash to tell PHP to look from the root namespace:\App\Models\Admin::create($record);
2. Fixing "Trait 'Illuminate\Notification\Notifiable' not found"
This one's a simple typo in your model's namespace reference! In Laravel 8, the Notifiable trait lives under the plural Notifications namespace, not the singular Notification.
Update your App/Models/Admin.php model:
<?php namespace App\Models; use Illuminate\Notifications\Notifiable; // Change Notification to Notifications use Illuminate\Foundation\Auth\User as Authenticatable; // use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Admin extends Authenticatable { use Notifiable; protected $guard = 'admin'; protected $fillable = [ 'name', 'type','email','password','image','status','created_at','updated_at', ]; protected $hidden = [ 'password', 'remember_token', ]; }
Final Steps to Verify
- Save all your modified files
- Run
composer dump-autoload(orcomposer duas you did before) to refresh the autoloader - Execute your seeder with:
php artisan db:seed # Or target the specific seeder directly: php artisan db:seed --class=AdminsTableSeeder
Quick Bonus Tip
Your password field in $adminRecords is empty—don't forget to hash it using Laravel's Hash facade! Update that line like this (and add the import to your seeder):
// In your AdminsTableSeeder, add this import: use Illuminate\Support\Facades\Hash; // Then update the password line: 'password' => Hash::make('your-secure-password'),
内容的提问来源于stack exchange,提问作者Agustine

