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

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 a use statement 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 the use statement, 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

  1. Save all your modified files
  2. Run composer dump-autoload (or composer du as you did before) to refresh the autoloader
  3. 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 07:30:06