1

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

3
  • 2
    Well you simply cannot. Its not possible to set a value to a file input. The value only gets set by user input. You can add a hidden field with the current image, and a checkbox to remove the image. Commented Oct 28, 2021 at 5:53
  • @GertB. Thanks for the answer. How do I make it not null? Commented Oct 28, 2021 at 5:57
  • If the user does not input a file, it will be null. handle that in the controller. Add a checkbox to remove the image if it must be possible to delete the image. Commented Oct 28, 2021 at 5:59

1 Answer 1

1

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");
Sign up to request clarification or add additional context in comments.

11 Comments

The question is "how can i add a value to a file input".
description "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."
Oh, I missed that you changed the $data variable to make sure it does not set it as null. Please explain what you've changed to improve your question.
@KUMAR in this code he sets a data array without image if there is no image is selected. This way the image will not be null in the database after saving.
@MohamedGamalEldin this is an error Undefined variable $file , where does $file->move( comes from?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.