Skip to content
Advertisement

Fetch image variable and display on input file

I am trying to update an image and other data in a database, but when I update only text data, the image value becomes null or empty.

<form action="/admin/settings/why-us/update/{{$data->id}}" enctype="multipart/form-data" method="POST">
@csrf
<input type="text" class="form-control" name="title" value="{{$data->title}}">
<input type="file" class="form-control" value="{{$data->image}}" name="image">
<button type="submit"  class="btn btn-success py-2 px-4 text-white">Update changes</button>
</form>

This a controller

public function updateWhyusPageSetting(Request $request,$id)
    {
        $title = $request->input('title');
        $image =  $image = $request->file('image');
        dd($image);

          if($request->hasFile('image')) {
                $image = $request->file('image');
                $filename = $image->getClientOriginalName();
                $image->move(public_path('/frontend/images/'), $filename);
                $image_upload = $request->file('image')->getClientOriginalName();
            }


          DB::table('features')
            ->where('id', $id)
            ->update([
                'title'  => $title, 
                'image' => $image_upload
            ]);
   
 
        

        Session::flash('flash_message', __('Why us data updated'));
        Session::flash('flash_type', 'success');

        return redirect()->back();
    }

When I input only the title, left out the image, and tried to dump using dd($image);, I got a null value.

When updating the image, it’s getting updated very well database.

Now, my question is, how do I make sure the value is captured in the input file <input type="file" class="form-control" value="{{$data->image}}" name="image"> so that when I update other data, it also sends the image value. NB: value=”{{$data->image}}” IS NOT capturing the data from database

Advertisement

Answer

Try this code

public function updateWhyusPageSetting(Request $request,$id){
   $data = [];
   $data['title'] = $request->input('title');
   if($request->hasFile('image')) {
       $image = $request->file('image');
       $image->move(public_path('/frontend/images/'),$imageName = $image->hashName()); //hashName() will generate image name with extension
       $data['image'] = $imageName; // here if user uploads an image, it will add to data array then add to DB.
   }

    DB::table('features')
       ->where('id', $id)
       ->update($data); // if a user uploaded an image will add. if not, a previous image will not change
    Session::flash('flash_message', __('Why us data updated'));
    Session::flash('flash_type', 'success');

        return redirect()->back();
    }

Please note you should delete the old images if you don’t need anymore

you can use this to delete an old image if you want

(new Filesystem())->delete("full/path/with/image/name.jpg");
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement