ma123456 ma123456 - 3 years ago 161
PHP Question

Load Current Image to Update in Edit Form (Using Form Binding in Laravel 5.5)

I am trying to load the user current image(Image URL saved in db)in Edit Form...
If new image in edit form is not selected i get this error...

Undefined variable: image_path

Here is my update function

public function update(Request $request,User $user)
{
$get_user = Auth::user();
if(Input::hasFile('image'))
{
$user_email = $get_user->email;
$image = request()->file('image');
$image_extension = $image->guessClientExtension();
$image_path = $image->storePubliclyAs('public/uploads/profile_images/images', $user_email . ".{$image_extension}");

}

$user->firstname = $request->get('firstname');
$user->lastname = $request->get('lastname');
$user->company= $request->get('company');
$user->address= $request->get('address');
$user->phone = $request->get('phone');
$user->country= $request->get('country');
$user->zipcode = $request->get('zipcode');
$user->city = $request->get('city');
$user->state = $request->get('state');
$user->skills = $request->get('skills');
$user->interests = $request->get('interests');
$user->about = $request->get('about');
$user->website = $request->get('website');
$user->image_url = $image_path;
$user->save();

$request->session()->flash('message.level', 'success');
$request->session()->flash('message.content', 'Profile Updated Successfully !');
return redirect()->route('dashboard');

}


Here is my Image Field in Edit Form..

{{ Form::label('image_url', 'Profile Image') }}
{{ Form::file('image' ,null, ['class' => 'form-control','id'=>'image_url']) }}


Please Help and give me a solution to handle a scenerio if user not selected the image in Edit form..

Answer Source

You can move $user->image_url = $image_path; inside if block if(Input::hasFile('image')).Error will return because in edit you are not selecting image so if block inside code not working so $image_path will be undefined

public function update(Request $request,User $user)
    {
        $get_user = Auth::user();
        if(Input::hasFile('image'))
        {
            $user_email = $get_user->email;
            $image = request()->file('image');
            $image_extension = $image->guessClientExtension();
            $image_path = $image->storePubliclyAs('public/uploads/profile_images/images', $user_email . ".{$image_extension}");
            $user->image_url = $image_path;
        }

        $user->firstname = $request->get('firstname');
        $user->lastname = $request->get('lastname');
        $user->company= $request->get('company');
        $user->address= $request->get('address');
        $user->phone = $request->get('phone');
        $user->country=  $request->get('country');
        $user->zipcode = $request->get('zipcode');
        $user->city = $request->get('city');
        $user->state = $request->get('state');
        $user->skills = $request->get('skills');
        $user->interests = $request->get('interests');
        $user->about = $request->get('about');
        $user->website = $request->get('website');

        $user->save();

        $request->session()->flash('message.level', 'success');
        $request->session()->flash('message.content', 'Profile Updated Successfully !');
        return redirect()->route('dashboard');

    }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download