动态增减字段场景下图片入库报错:Call to a member function hashName() on array
问题描述
页面存在可动态增减的输入字段,未上传图片时功能运行正常,上传图片时出现错误:Call to a member function hashName() on array,无法将图片存入数据库。
相关代码
控制器数据添加代码
$products = $request->day_book_id; for ($i=0; $i<count($products); $i++) { $items = new Daybooks_units(); $items->debit= $request->debit[$i]; $items->creditor= $request->creditor[$i]; $items->data= $request->data[$i]; $items->reference= $request->reference[$i]; $items->files= $request->reference[$i]; $items->files = uploadImage('filesDayBooks',$request->images)[$i]; $items->dayBook_code= $request->dayBook_code[$i]; $items->fullAccounting_id= $request->fullAccounting_id[$i]; $items->day_book_id= $request->day_book_id[$i]; $items->branch_id= $request->branch_id[$i]; $items->save(); }
Blade视图代码
<td style="padding:3px" > <input type="text" class="search" name="search" required> <select class="form-control search_list getData" id="search_list" name="fullAccounting_id[]"></select> @error('fullAccounting_id') <div class="alert alert-danger" role="alert"> {{ $message }} </div> @enderror </td> <td style="display:none" > <input type="text" value=" " class=" form-control putParentId" name="ParentOfAccounting_manual_id[]"> </td> <td style="display:none"> <input type="text" value=" " class=" form-control putCode" name="code_accountingManuals[]"> </td> <td style="display:none"> <input type="text" value=" " class=" form-control debtor_creditor" name="debtor_creditor[]"> </td> <td style="padding:3px"> <input type="text" value="0" class=" form-control totaldebit" onchange="getTotalRowPrice(this , '{{-500}}' )" name="debit[]"> @error('debit') <div class="alert alert-danger" role="alert"> {{ $message }} </div> @enderror </td> <td style="width:100px;"> <input type="text" onchange="getTotalRowPrice(this , '{{-500}}' )" value="0" class=" form-control totalcreditor" name="creditor[]"> @error('creditor') <div class="alert alert-danger" role="alert"> {{ $message }} </div> @enderror </td> <td style="padding:3px"> <input type="text" value="" class=" form-control" name="data[]"> </td> <td style="width:100px;"> <select name="branch_id[]" required class="form-control"> <option></option> @foreach($costCenters as $costCent) <option value="{{$costCent->id}}">{{$costCent->name}}</option> @endforeach </select> @error('branch_id') <div class="alert alert-danger" role="alert"> {{ $message }} </div> @enderror </td> <td style="padding:3px"> <input type="text" class="form-control " name="reference[]" value=""> </td> <td style="display:none;"> <input type="text" class="form-control " name="id_unit[]" value="{{ $id_unit }}"> </td> <td style="display:none"> <input type="text" class="form-control " name="daybooks_units[]" value="daybooks_units"> </td> <td style="padding:3px"> <input type="file" name="images[]" multiple class="form-control" id="customFile"> </td> <td style="padding:3px"> <input type="button" class="btn btn-success" name="add" id="add" value="add" style="color: white"> </td>
场景说明
表单为动态可增减的多行结构,每行包含输入框、下拉选择、文件上传按钮,用于提交多条关联数据及对应图片。
错误原因分析
- 文件字段属性错误:视图中文件输入框添加了
multiple属性,导致所有行的上传文件被合并为一个大数组,而控制器循环时直接将整个数组传入uploadImage函数,函数内部尝试对数组调用hashName()方法,触发错误。 - 逻辑顺序错误:控制器中先给
$items->files赋值为参考字段内容,后续又重新赋值,属于冗余代码;且未对文件是否存在做判断,空文件场景会引发异常。 - 表单缺失必要属性:若表单未添加
enctype="multipart/form-data",将无法正确接收上传的文件。
解决方案
1. 调整视图文件输入字段
移除multiple属性,确保每行仅对应一个文件上传,同时给每个文件输入框设置唯一ID避免冲突:
<td style="padding:3px"> <input type="file" name="images[]" class="form-control" id="customFile_{{$loop->index ?? 0}}"> </td>
(动态新增行时,需保证生成的ID唯一,可通过JS动态拼接序号实现)
2. 修改控制器代码
先批量处理所有上传图片,再循环关联到对应数据行:
// 先处理所有上传图片,生成路径数组 $uploadedImages = []; if ($request->hasFile('images')) { foreach ($request->images as $image) { $uploadedImages[] = $image->isValid() ? uploadImage('filesDayBooks', $image) : null; } } $products = $request->day_book_id; for ($i=0; $i<count($products); $i++) { $items = new Daybooks_units(); $items->debit= $request->debit[$i]; $items->creditor= $request->creditor[$i]; $items->data= $request->data[$i]; $items->reference= $request->reference[$i]; // 关联对应行的图片路径,无图片则存null $items->files = $uploadedImages[$i] ?? null; $items->dayBook_code= $request->dayBook_code[$i]; $items->fullAccounting_id= $request->fullAccounting_id[$i]; $items->day_book_id= $request->day_book_id[$i]; $items->branch_id= $request->branch_id[$i]; $items->save(); }
3. 确保uploadImage函数正确处理单个文件
函数需接收单个UploadedFile对象,示例实现:
function uploadImage($folder, \Illuminate\Http\UploadedFile $file) { $fileName = $file->hashName(); $file->storeAs($folder, $fileName, 'public'); return $fileName; // 可根据需求返回完整存储路径 }
4. 补充表单必要属性
在表单标签添加enctype="multipart/form-data":
<form method="POST" action="{{ route('your.route') }}" enctype="multipart/form-data"> @csrf <!-- 表单内容 --> </form>
内容的提问来源于stack exchange,提问作者user20626943
相关产品推荐
相关产品推荐

