Php 上传时,Laravel文件名未正确保存在数据库中
我想上传并保存文件,如果请求有化身字段。但是,当我上传时,该文件正以生成的文件名在digital ocean spaces中正确上传。但在我的数据库中,它的保存方式是-C:\Users\debar\AppData\Local\Temp\php29DC.tmpPhp 上传时,Laravel文件名未正确保存在数据库中,php,laravel,Php,Laravel,我想上传并保存文件,如果请求有化身字段。但是,当我上传时,该文件正以生成的文件名在digital ocean spaces中正确上传。但在我的数据库中,它的保存方式是-C:\Users\debar\AppData\Local\Temp\php29DC.tmp $validator = Validator::make($request->all(), [ 'name' => 'string', 'mobile' =>
$validator = Validator::make($request->all(), [
'name' => 'string',
'mobile' => 'numeric|nullable',
'email' => 'email|nullable',
'bio' => 'string|nullable',
'school' => 'string|nullable',
'college' => 'string|nullable',
'country_id' => 'integer|nullable',
'state_id' => 'integer|nullable',
'city_id' => 'integer|nullable',
'avatar' => 'file|nullable'
]);
if($validator->fails()) {
$errors = ['validation_error' => $validator->errors()];
return response()->json($errors, 400);
}
if($request->has('avatar')){
$extension = $request->file('avatar')->extension();
$mimeType = $request->file('avatar')->getMimeType();
$fileName = time().'.'.$extension;
Storage::disk('do_spaces')->putFileAs('doctor-profile', $request->file('avatar'), $fileName);
$request->avatar = $fileName;
}
$doctor = Auth::guard('doctor_api')->user();
$doctor->update($request->all());
return $doctor;
当我dd($request->avatar)时
当您在digital ocean space中保存文件时,您正在创建一个新文件名“$fileName”,而在数据库中更新时,您已将您的请求参数置于数据库中更新之前更改您的请求参数,并将您的新文件名置于数据库中。我尝试如下操作-$request->merge(['avatar'=>'abc']);但是仍然不起作用添加$request在
更新
方法之前添加此行:$request->merge(['avatar'=>$fileName??NULL])我试过了,但没用。我已经用dd更新了这个问题($request->avatar)
Illuminate\Http\UploadedFile {#380
-test: false
-originalName: "dummy-girl.jpg"
-mimeType: "image/jpeg"
-error: 0
#hashName: null
path: "C:\Users\debar\AppData\Local\Temp"
filename: "phpCD78.tmp"
basename: "phpCD78.tmp"
pathname: "C:\Users\debar\AppData\Local\Temp\phpCD78.tmp"
extension: "tmp"
realPath: "C:\Users\debar\AppData\Local\Temp\phpCD78.tmp"
aTime: 2020-10-16 15:45:08
mTime: 2020-10-16 15:45:08
cTime: 2020-10-16 15:45:08
inode: 19140298416425963
size: 10227
perms: 0100666
owner: 0
group: 0
type: "file"
writable: true
readable: true
executable: false
file: true
dir: false
link: false
linkTarget: "C:\Users\debar\AppData\Local\Temp\phpCD78.tmp"
}