I'm building a Laravel
-nova application and I have a <form>
where it should be possible to upload multiple files. So I have to tables - single_applications
and single_application_files
. So, when I fill out the form the data gets posted correctly except the files and I have absolutely no clue why e.g. I don't know what I'm doing wrong.
Here is the blade template:
<form method="POST" action="{{ route('application.store') }}">
<div class="input-group mb-3">
<input name="name" type="text" class="form-control" placeholder="Name" autocomplete="name" required>
</div>
//...and a bunch more
//...then the file input
<div class="form-group col">
<div class="custom-file file">
<input type="file" class="custom-file-input" id="attachment-1" name="attachment_cv[]" accept=".pdf,.doc,.docx,application/msword,.png,.jpg,.jpeg,.gif">
<label class="custom-file-label" for="attachment-1">Add File</label>
<div class="addBtnContainer">
<a href="#" data-js="add" class="js-addFileField">+ Add</a>
</div>
</div>
</div>
</form>
Due to usability I don't want the <input type="file" multiple />
, instead the user can click on add
and with a little help from jquery
, another file input field appears.
My SingleApplication
-Model looks like this:
class SingleApplication extends Model
{
protected $fillable = [
'avatar', 'name', 'email', 'phone', 'address', 'zipcode', 'city',
'education', 'about', 'birthyear', 'job_category', 'status','vehicles', 'worktime', 'clothing'
];
}
And my SingleApplicationFile
-Model looks like this
class SingleApplicationFile extends Model
{
protected $fillable = [
'attachment_cv', 'files_id', 'attachment_cv_name', 'attachment_cv_size', 'single_applications_id'
];
}
So far so good, so I do use axios
to do post the data, so here is how it looks:
$('#singleApplication button[type="submit"]').click(function(e) {
e.preventDefault();
var formData = new FormData();
formData.append(
"name",
$("#singleApplication")
.find('input[name="name"]')
.val()
);
// ...etc. etc. the same with all other fields
// HERE I FETCH THE ATTACHMENT FILES
var attFiles = [];
$('input[name="attachment_cv[]').each(function() {
attFiles.push($(this).prop("files")[0]);
});
formData.append("attachment_cv", attFiles);
axios.post($("#singleApplication form").attr("action"), formData)
.then(response => {
})
.catch(error => {
console.log(error.response.data); // DEBUG
});
});
});
Now it comes to the controller, and here is what I think something goes wrong.
use App\SingleApplication;
use App\SingleApplicationFile;
class SingleApplicationsController extends Controller
{
public function store()
{
$application = SingleApplication::create([
'email' => request()->email,
'name' => request()->name,
'avatar' => request()->avatar,
'phone' => request()->phone,
'address' => request()->address,
'zipcode' => request()->zipcode,
'city' => request()->city,
'birthyear' => request()->birthyear,
'job_category' => request()->job_category,
'education' => request()->education,
'about' => request()->about,
'vehicles' => request()->vehicles,
'worktime' => request()->worktime,
'clothing' => request()->clothing,
'status' => request()->status,
]);
if (request()->hasFile('attachment_cv')) {
$files = request()->file('attachment_cv');
foreach ($files as $file) {
$filename = $file->getClientOriginalName();
$extension = $file->getClientOriginalExtension();
$filesize = $file->getSize();
$path = Storage::disk('local')->put('attachments/application_files', request()->file($filename));
$application_files = SingleApplicationFile::create([
'files_id' => $application->id,
'single_application_id' => $application->id,
'attachment_cv' => $path,
'attachment_cv_name' => $extension,
'attachment_cv_size' => $filesize,
]);
new SingleApplicationFile($application_files);
return response()->json('OK', 200);
}
}
}
So, can someone maybe tell me, what Im doing wrong? As mentioned before, all data go through except the files, the table in the database remains empty.
UPDATE
For some reason the request does not pass the line
if (request()->hasFile('attachment_cv')) {}
how can that be?