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();
}