我将这个AdminFactory.php文件放在database->factories目录中:
<?php
namespace Database\Factories;
use App\Models\Backend\Admin;
use Illuminate\Database\Eloquent\Factories\Factory;
use Illuminate\Support\Str;
class AdminFactory extends Factory
{
/**
* The name of the factory's corresponding model.
*
* @var string
*/
protected $model = Admin::class;
/**
* Define the model's default state.
*
* @return array
*/
public function definition()
{
return [
'name' => 'Admin',
'email' => 'admin@admin.com',
'email_verified_at' => now(),
'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
'remember_token' => Str::random(10),
];
}
}当我尝试运行以下命令时:php artisan migrate --seed
它在终端中显示此错误消息:

发布于 2022-04-03 16:40:11
试图在包中使用工厂时遇到了这个问题。我通过重写HasFactory::newFactory()方法来解决这个问题。
class LegacyCategory extends Model
{
use HasFactory;
...
/**
* Create a new factory instance for the model.
*
* @return \Illuminate\Database\Eloquent\Factories\Factory
*/
protected static function newFactory()
{
return new LegacyCategoryFactory();
}
}https://stackoverflow.com/questions/65539935
复制相似问题