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

Laravel Livewire文件上传报错:Call to a member function extension() on null

解决Laravel Livewire文件上传中Call to a member function extension() on null错误

问题根源

在Livewire组件中,通过wire:model="file"绑定的文件会直接存储在组件的公共属性$file中,而非通过Request实例获取。你当前代码中使用$request->file尝试获取文件,导致获取到null,进而触发错误。

具体修复步骤

  1. 声明组件公共属性
    在你的Livewire组件类顶部添加公共属性,用于接收上传的文件:
public $file;
  1. 修改store方法
    移除Request $request参数,直接使用组件的$file属性处理文件:
public function store()
{
    // 验证PDF文件并写入数据库
    $this->validate([
        'file' => 'required|mimes:pdf|max:2048',
    ]);

    // 生成文件名
    $imageName = time() . '.' . $this->file->extension();
    // 移动文件到指定目录
    $this->file->move(public_path('images'), $imageName);

    Breakdown::updateOrCreate(['id' => $this->breakdown_id], [
        'Identification_No' => session('idno'), // 直接使用session辅助函数,无需实例化Session类
        'Breakdown_date' => $this->Breakdown_date,
        'Breakdown_problem' => $this->Breakdown_problem,
        'Breakdown_parts' => $this->Breakdown_parts,
        'Description' => $this->Description,
        'Complete_date' => $this->Complete_date,
        'Action_by' => $this->Action_by,
        'Reviewed_by' => $this->Reviewed_by,
        'Remarks' => $this->Remarks,
        'filename' => $imageName,
    ]);

    session()->flash('message',
        $this->breakdown_id ? '故障记录更新成功。' : '故障记录创建成功。');

    $this->closeModal();
    $this->closeimpModal();
    $this->resetInputFields();
}
  1. 额外注意事项
  • 确保public/images目录存在且具备写入权限,若不存在可通过以下命令创建并设置权限:
mkdir -p public/images && chmod 755 public/images

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 13:20:28