artisticre's avatar

Image won't upload

Everything updates with this except the image. If I dd($request->image) outside the if statement, it displays the image name but if its inside the if statement it does not. not sure what to do

Update Function

public function update(Request $request, $id)
    {
         $profile = Profile::find($id);
   
        $this->validate($request, [
            'address'=>'required|min:4|max:255',
            'apt'=>'nullable:min:4|max:50',
            'city'=>'required|min:4|max:255',
            'state'=>'required|min:4|max:255',
            'zipcode'=>'required|min:4|max:255',
            'homephone'=>'required|min:4|max:255',
            'mobile'=>'nullable|min:4|max:255',
            'occupation'=> 'required|min:4|max:255',
            'over18'=>'required',
            'homechurch' => 'required|min:4|max:255',
            'homechurchcity'=> 'required|min:4|max:255',
            'pastor'=>'required|min:4|max:255',
            'howoftenattend'=>'required|min:4|max:255',

          ]);
     
         
          if($request->hasFile('image')){
            $image = $request->file('image');
            $filename = time() .'.'.$image->getClientOriginalExtension();
            $location = public_path('frontenddashboard/images/profile/'.$filename);
            Image::make($image)->resize(300,300)->save($location);
           
            $oldFilename = $profile->image;
            $profile->image = $filename;
            if ($profile->image !== 'default.png') {
            File::delete(public_path('frontenddashboard/images/profile/'.$oldFilename));
        }  
    }
 
          $profile->address = $request->address;
          $profile->apt = $request->apt;
          $profile->city = $request->city;
          $profile->state = $request->state;
          $profile->zipcode = $request->zipcode;
          $profile->homephone = $request->homephone;
          $profile->mobile = $request->mobile;
          $profile->occupation = $request->occupation;
          $profile->over18 = $request->over18;
          $profile->homechurch = $request->homechurch;
          $profile->homechurchcity = $request->homechurchcity;
          $profile->pastor = $request->pastor;
          $profile->howoftenattend = $request->howoftenattend;

          
           
          $profile->save(); 
           
          Toastr::success('Profile Has Been Updated', 'Success', ["positionClass" => "toast-top-right"]);
        return redirect()->route('profile.index');
    }

input from edit view

<img class="img-thumbnail" style="height:100px;width:100px" src="{{ asset('frontenddashboard/images/profile')}}/{{$profile->image}}" alt="">
                    <input type="file" class="form-control" name="image">       
0 likes
1 reply
MohamedTammam's avatar
Level 51

you need to set enctype in your input.

<input type="file" class="form-control" name="image" enctype="multipart/form-data">

Please or to participate in this conversation.