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

Laravel-8与Maatwebsite-3.1 Excel导入报错:Call to undefined function App\Imports\create() 解决方法咨询

解决Laravel Excel导入中Call to undefined function create()的错误

这个错误的原因很直接——Laravel中并没有全局的create()函数,你应该使用Eloquent模型自带的静态create()方法来创建新记录。

修复步骤:

  1. 替换错误的create()调用
    把你的代码里这一行:
$employee = create(Employee::class, $employee_data);

改成直接调用模型的静态create()方法:

$employee = Employee::create($employee_data);
  1. 确保模型支持批量赋值
    别忘了在Employee模型中设置$fillable属性,包含你要批量插入的字段,否则会触发Laravel的批量赋值保护错误:
// app/Models/Employee.php
class Employee extends Model
{
    protected $fillable = [
        'first_name',
        'other_name',
        'last_name',
        'email',
        'dob',
        'user_id' // 后续会更新这个字段,建议也加入
    ];
}

额外的小问题提醒:

你的代码里更新User的部分有个潜在bug:User::update(...)会批量更新所有用户的信息,这绝对不是你想要的。应该改成通过email定位到具体用户再更新:

// 原错误代码
$user = User::update([
    'first_name' => $employee->first_name,
    'last_name' => $employee->last_name,
    'active' => 1,
]);

// 修复后
$user = User::where('email', $employee->email)->update([
    'first_name' => $employee->first_name,
    'last_name' => $employee->last_name,
    'active' => 1,
]);

这样修改后,不仅能解决create()函数未定义的错误,还能避免误更新所有用户的问题。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.30 06:24:05