在laravel中,我在编辑视图中有一个名为Profile Picture的字段,每当我单击“编辑”按钮时,我都会从数据库中获得所有值,但没有图像;如果每次我单击“提交”按钮,我必须上传图像,否则我将无法进一步处理。如果我不上传新图像,则表单将自动考虑旧的个人资料图片
我的刀片文件就像
<div class="col-md-6">
<div class="form-group">
<label for="photo">Profile Picture :<span class="danger">*</span> </label>
<div class="row">
<div class="col-md-9">
<input type="file" class="form-control" id="file" name="file">
</div>
<div class="col-md-3">
@foreach ($empProfilePic as $empProfilePicture)
@if($employee->id == $empProfilePicture->id)
<img src="uploads/images/{{ $empProfilePicture->file }}" id="profile-img-tag" height="100" width="100">
@endif
@endforeach
</div>
</div>
</div>
</div>
控制器文件
public function updateEmployee(Request $request, $id)
{
$employee = User::find($id);
//Get inputs for personal detail
$firstName = $request->get('firstName');
$middleName = $request->get('middleName');
$lastName = $request->get('lastName');
$gender = $request->get('gender');
$city = $request->get('city');
$state = $request->get('state');
$localAddress = $request->get('localAddress');
$permanentAddress = $request->get('permanentAddress');
$personalEmail = $request->get('personalEmail');
$mobileNumber = $request->get('mobileNumber');
$companyEmail = $request->get('companyEmail');
$empId = $request->get('empId');
$department = $request->get('department');
$designation = $request->get('designation');
$status = $request->get('status');
$totalExperience = $request->get('totalExperience');
$aboutMe = $request->get('aboutMe');
$roleName = $request->get('role');
$role = $request->get('role');
if ($role == 'hr')
{
$role = '5c8a51ed650fbd5398503043';
}
else
{
$role = '5c8a51ed650fbd5398503044';
}
// //Store Image In Folder
$file = $request->file('file');
$name = $file->getClientOriginalName();
$file->move('uploads/images', $name);
if (file_exists(public_path($name = $file->getClientOriginalName())))
{
unlink(public_path($name));
};
$accountHolderName = $request->get('accountHolderName');
$accountNumber = $request->get('accountNumber');
$bankName = $request->get('bankName');
$ifscCode = $request->get('ifsc_code');
$panNumber = $request->get('panNumber');
$branchName = $request->get('branchName');
//Enter in database
$employee->role_id = $role;
$employee->role_name = $roleName;
$employee->username = $firstName;
//Update Image
$employee->file = $name;
$employee->personal_email = $personalEmail;
$employee->company_email = $companyEmail;
$employee->status = $status;
$personalDetail = ([
'emp_id' => $empId,
'first_name' => $firstName,
'middle_name' => $middleName,
'last_name' => $lastName,
'gender' => $gender,
'city' => $city,
'state' => $state,
'local_address' => $localAddress,
'permanent_address' => $permanentAddress,
'personal_email' => $personalEmail,
'mobile_number' => $mobileNumber,
'department' => $department,
'designation' => $designation,
'total_experience' => $totalExperience,
'about_me' => $aboutMe,
]);
$bankDetail = ([
'account_holder_name' => $accountHolderName,
'account_number' => $accountNumber,
'bank_name' => $bankName,
'ifsc_code' => $ifscCode,
'pan_number' => $panNumber,
'branch_name' => $branchName,
]);
$employee->status = $status;
$employee->personal_detail = $personalDetail;
$employee->bank_detail = $bankDetail;
$employee->save();
return redirect('list-of-employees')->with('Success', 'Data Updated Successfully!');
}
任何人都会帮忙的,谢谢您
答案 0 :(得分:2)
尝试一下。如果您使用post方法获取文件,则仅设置雇员的file变量。
// //Store Image In Folder
if (isset($_FILES['file'])) {
$file = $request->file('file');
$name = $file->getClientOriginalName();
$file->move('uploads/images', $name);
if (file_exists(public_path($name = $file->getClientOriginalName())))
{
unlink(public_path($name));
};
//Update Image
$employee->file = $name;
}
//Enter in database
$employee->role_id = $role;
$employee->role_name = $roleName;
$employee->username = $firstName;
答案 1 :(得分:1)
在控制器中:
更新文件的代码:
仅当您选择要更新的文件时,它才会更新。
在这里,我使用可以用您的模型替换的Employee模型
public function update(Request $request, $id){
$employee = Employee::find($id);
if($request->file != ''){
$path = public_path().'/uploads/images/';
//code for remove old file
if($employee->file != '' && $employee->file != null){
$file_old = $path.$employee->file;
unlink($file_old);
}
//upload new file
$file = $request->file;
$filename = $file->getClientOriginalName();
$file->move($path, $filename);
//for update in table
$employee->update(['file' => $filename]);
}
}