Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

lily90's avatar

Problem with retrieving file name is update form field [Laravel]

I have problem with file name value in database can't be retrieved in update form. The file are successfully submited in my database and my public storage and the view list function in my homepage. But when I try to edit a form with file, the file name value did not retrieve which the cause me an error of I cant update my form because of the event_image field are required in my database.

update

 <form class="form-horizontal" action="{{route('admin.event.update',$event->id)}}" method="POST" enctype="multipart/form-data">
              @csrf

<div class="form-group row">
                   <label for="event_image" class="col-sm-2 col-form-label">Event Picture</label>
                       <div class="col-lg-10"></label>
                          <input type="file" id="event_image" name="event_image"value="{{$event->event_image}}" ><br/>       
                         </div>
</div>

eventController

 public function update(Request $request)
    {
        $this->validate($request,[
            'event_image'=>'image|nullable|max:1999',
            'event_availability'=>'required',
        ]);
//handle the file upload
         if($request->hasFile('event_image')){   // to check if user has opted to upload the file
        
            // get filename with extention
            $filenameWithExt = $request->file('event_image')->getClientOriginalName();

            //get just filename
            $filename = pathinfo($filenameWithExt, PATHINFO_FILENAME);
            //get ext (extention)
            $extension =$request->file('event_image')->getClientOriginalExtension();
            //filename to store
            $fileNameToStore = $filename .'_'.time().'.'.$extension;
            //upload image
            $path = $request->file('event_image')->storeAs('public/event_images',$fileNameToStore);
         }
 $event = new Event;
if($request->hasFile('event_image')){
            $event->event_image =$fileNameToStore;
             }
$event->save();
}



0 likes
13 replies
thewebartisan7's avatar

Remove value from file input

<input type="file" id="event_image" name="event_image">

Then in your controller:

        if($request->event_image instanceof \Illuminate\Http\UploadedFile) {

        	// Optionally delete existing file
        	if($event->event_image) {
            		\Illuminate\Support\Facades\Storage::disk('public')->delete($event->event_image);
        	}

            $event->event_image = $request->event_image->store('event_images', 'public');
        }


// the rest of code for update

Notice that using just store() method it will create a unique name, so no need to make check that you are doing.

Sinnbeck's avatar

You are missing a space in the input definition (betweeen image" and value)

 id="event_image" name="event_image" value="
lily90's avatar
public function edit($id)
    {
        $event= Event::find($id);

        return view('admin.event.edit')->with('event', $event);
    }

lily90's avatar

corrected it but still the file name did not show up.

laracoft's avatar

@lily90

  1. Can you do a ctrl-u to show the HTML source code in your browser and show us the form's code?
  2. And confirm that the filename is indeed stored in your database record?
lily90's avatar

i don't want to delete the file. I just want to retrieve the file value name. So that user can update if they want. If they don't want they can leave it and update another field in the form.

laracoft's avatar

Ah! got it. Your <input> is of file type. It does not accept value. You have to restructure the way you do edit().

laracoft's avatar

If you just wish to change filename (which I'm not sure is a good idea), you have to change the type to text.

lily90's avatar

I did the ctrl-u and yeah it did store the value

  <input type="file" id="event_image" name="event_image" value="xxxxx.jpg" 

the value name is same as the value inside the database. So does it mean I can't change the image?

laracoft's avatar
laracoft
Best Answer
Level 27

@lily90 you could show the existing images filenames as text with <a href> delete links (DELETE) and have a blank <input type="file"> for user to upload new images (CREATE).

DELETE + CREATE = CHANGE

Caveat: use of <a href> delete links is not considered as a safe practice. For safer approaches, you need more advanced javascripts. But you get the idea.

1 like

Please or to participate in this conversation.