如何在laravel 5.6中更新图片

时间:2018-11-04 11:29:57

标签: php laravel

我想在laravel 5.6中更新图像。

在我的应用程序中,图像正在正确上传并插入数据库中,但是当我要更新图像时,它无法更新旧图像。

如何用其他字段更新图像?

这是我的观点:

<form method="post" action="{{ route('employee.update', $employee->id) }}" enctype="multipart/form-data">
    @csrf
    @method("PATCH")
    <input type="hidden" name="profile_old_image" value="{{$employee->profile_pic}}">
    <div>
        <input type="file" name="profile_pic" class="form-contrle" >
    </div> 
    <div>
        <input placeholder='Your Name' type='text' name='name' value="{{$employee->name}}" class="form-control">
    </div>         
</form>

这是我的控制者:

public function update(Request $request, $id)
    {
        $rules = [
            'name' => 'required',
            'father_name' => 'required',
            'phone_no' => 'required',
            'cnic' => 'required'
        ];


        $validate = $this->validate($request, $rules);

        $file = $request->file('profile_pic');
        if($file == '')
        {
            $profile_pic = $request->profile_old_image;
        }
        else
        {
            $file = $request->file('profile_pic');
            $fileName = time().'.'.$file->getClientOriginalExtension();
            $destinationPath = public_path('/admin/images/employees');
            $file->move($destinationPath,$fileName);
            $profile_pic = '/admin/images/employees/'.$fileName; 

        }
        //dd($request->all());
        $employee = Employee::find($id);
        $employee->name = $request->name;
        $employee->father_name = $request->father_name;
        $employee->phone_no = $request->phone_no;
        $employee->cnic = $request->cnic;
        $employee->desg_id = $request->desg_id;
        $employee->profile_pic = $profile_pic;
        $employee->save();
        return redirect('admin/employee')->with('message', 'Records Successfully Updated');
    }

0 个答案:

没有答案