I have been trying to update a form that has a resume file input. I want users to only change the file if they are make mistake of uploading a wrong resume but the page refrresh and when i debug using dd i can not see in my json respond the cv input with all other fields here is my current code
InterviewController.php
public function update(Request $request, $id)
{
// Validate the form data
$request->validate([
'candidat_name' => 'required|string',
'poste' => 'required|string',
'interview_time' => 'required|date_format:H:i',
'interview_date' => 'required|date',
'cv' => 'nullable|file|mimes:pdf,doc,docx|max:2048',
'description' => 'required|string',
]);
// Find the existing Interview model instance
$interview = Interview::findOrFail($id);
// Update interview attributes
$interview->candidat_name = $request->input('candidat_name');
$interview->poste = $request->input('poste');
$interview->interview_time = $request->input('interview_time');
$interview->interview_date = $request->input('interview_date');
$interview->description = $request->input('description');
// Handle file upload if a new CV is provided
if ($request->hasFile('cv')) {
// Delete the existing CV file if it exists
if ($interview->cv) {
Storage::delete('public/' . $interview->cv);
}
$cv = $request->file('cv');
$cvPath = $cv->store('cvs', 'public');
$interview->cv = $cvPath;
} else {
// If no new file is provided, keep the existing file path
$interview->cv = $interview->cv;
}
// Save the updated interview
$interview->save();
// Redirect with a success message
return redirect()->route('interviews.index')->with('success', 'Interview updated successfully.');
}
index.blade.php
Here is my form
@foreach ($interviews as $interview)
<div class="modal fade bd-example-modal-lg" id="editModal-{{ $interview->id }}" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="myLargeModalLabel">Modifier Informations</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close">
<svg aria-hidden="true" xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="feather feather-x"><line x1="18" y1="6" x2="6" y2="18"></line><line x1="6" y1="6" x2="18" y2="18"></line></svg>
</button>
</div>
<div class="modal-body">
<form action="{{ route('interviews.update', $interview->id) }}" method="POST" enctype="multipart/form-data">
@csrf
@method('PUT')
<div class="row mb-3">
<div class="col">
<label for="">Candidat</label>
<input type="text" class="form-control form-control-sm" name="candidat_name" id="candidat_name" value="{{ $interview->candidat_name }}" placeholder="Nom & Prénom">
</div>
<div class="col">
<label for="">Poste</label>
<select class="form-select form-control-sm" name="poste" id="poste" required>
<option value="" disabled selected>Sélectionner Poste</option>
@foreach ($jobs as $job)
<option value="{{ $job->poste }}" {{ $interview->poste == $job->poste ? 'selected' : '' }}>{{ $job->poste }}</option>
@endforeach
</select>
</div>
</div>
<div class="row mb-2">
<div class="col">
<label for="">Heure d'entretien</label>
<input type="time" class="form-control form-control-sm" name="interview_time" id="interview_time" value="{{ $interview->interview_time }}" placeholder="Nom & Prénom">
</div>
<div class="col">
<label for="">Date d'entretien</label>
<input type="date" class="form-control form-control-sm" name="interview_date" id="interview_date" value="{{ $interview->interview_date }}">
</div>
</div>
<div class="mb-3">
<label for="">Curriculum Vitae</label>
<input class="form-control file-upload-input" type="file" name="cv" id="formFile">
</div>
<div class="form-group mb-4">
<label for="exampleFormControlTextarea1">Description</label>
<textarea class="form-control" id="exampleFormControlTextarea1" rows="3" name="description">{{ $interview->description }}</textarea>
</div>
</div>
<div class="modal-footer">
<input type="button" value="{{__('Annuler')}}" class="btn btn-dark" data-bs-dismiss="modal">
<input type="submit" value="{{__('Modifier')}}" class="btn btn-primary">
</div>
</form>
</div>
</div>
</div>
@endforeach
2
Answers
You don’t need to use the
..else{..}
block.if(){}
block alone will check if a file will be uploaded or not. if no file is getting uploaded then you don’t even need to update$interview->cv
field since there is no need to update the previous value and then updated theDB-column
with same value.As per documentation
store()
returned the/path-to-the-file
.So you also don’t need to explicitly define/public
inStorage::delete(...)
Please check the the documentation for further information on
store()
.https://laravel.com/docs/11.x/filesystem#file-uploads
As per the conversation in the
chat
and after your clarification I modified the code given below.Use this code in place of yours code.Try this.I think it will resolve the issue.If not then let me know.
Try to increase the post_max_size value to a higher value.
Try to check the server configuration, like
in
file.
Sometimes if the post size (Including file and other form data) increases the setting post size in php.ini file, data will not update.
Surprisingly it will not throw any error.
Note: If you are working local and using Servers like XAMP, WAMP you should restart the services.