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

Laravel执行db:seed报错:找不到User::factory()方法

问题:Laravel Seed 执行时出现 Call to undefined method Illuminate\Foundation\Auth\User::factory() 错误

问题场景

我尝试使用以下代码为两张表执行数据填充:

<?php

namespace Database\Seeders;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use Illuminate\Foundation\Auth\User;
use App\Models\Role;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        User::factory(10)->create();

        $roles = [
            'dps',
            'tank',
            'healer'
        ];

        foreach ($roles as $role) {
            Role::create([
                'name' => $role,
            ]);
        }

        foreach(User::all() as $user) {
            foreach (Role::all() as $role) {
                $user->$roles()->attach($role->id);
            }
        }
    }
}

执行php artisan db:seed时,返回错误:Call to undefined method Illuminate\Foundation\Auth\User::factory()

查看项目的App\Models\User模型,已明确引入HasFactory trait:

<?php

namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Laravel\Jetstream\HasProfilePhoto;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens;
    use HasFactory;
    use HasProfilePhoto;
    use Notifiable;
    use TwoFactorAuthenticatable;

    /**
     * The attributes that are mass assignable.
     *
     * @var string[]
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array
     */
    protected $hidden = [
        'password',
        'remember_token',
        'two_factor_recovery_codes',
        'two_factor_secret',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    /**
     * The accessors to append to the model's array form.
     *
     * @var array
     */
    protected $appends = [
        'profile_photo_url',
    ];

    public function monstros() {
        return $this->hasMany('App\Models\Monstro');
    }

    public function arsenais() {
        return $this->hasMany('App\Models\Arsenal');
    }

    public function itens() {
        return $this->hasMany('App\Models\Item');
    }

    public function roles() {
        return $this->belongsToMany(Role::class);
    }
}

我已经尝试过重启artisan、更新框架、执行dump-autoload,但问题仍未解决,请问我遗漏了什么?


问题原因及解决方法

1. 引入了错误的User类

你在Seeder中引入的是框架原生的Illuminate\Foundation\Auth\User类,这个类并没有集成HasFactory trait,而你的项目实际使用的是自定义的App\Models\User模型(已引入HasFactory)。

修正:
将Seeder中的:

use Illuminate\Foundation\Auth\User;

替换为:

use App\Models\User;

2. 关联调用语法错误

在关联用户与角色的循环中,你错误地使用了数组变量$roles来调用关联方法,正确的应该是调用模型中定义的roles()关联方法。

修正:
将:

$user->$roles()->attach($role->id);

替换为:

$user->roles()->attach($role->id);

修正后的完整Seeder代码

<?php

namespace Database\Seeders;
// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;
use App\Models\User;
use App\Models\Role;

class DatabaseSeeder extends Seeder
{
    /**
     * Seed the application's database.
     *
     * @return void
     */
    public function run()
    {
        User::factory(10)->create();

        $roles = [
            'dps',
            'tank',
            'healer'
        ];

        foreach ($roles as $role) {
            Role::create([
                'name' => $role,
            ]);
        }

        foreach(User::all() as $user) {
            foreach (Role::all() as $role) {
                $user->roles()->attach($role->id);
            }
        }
    }
}

内容的提问来源于stack exchange,提问作者Nathan Rodrigues

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.10 23:15:41