I'm trying to upload multiple files for a single row using Laravel and Ajax. For a single image, it works good, but when I try to upload multiple images, always getting error: "The file must be a file of type: jpeg, png, bmp, gif, svg, docx, xlsx, pdf." I want to do something like that:
id | client_id | files
---|-----------|------
1 | 1 | image.png, test.pdf
---|-----------|------
2 | 2 | imag3.png, test.docx
tasks.blade.php
<form method="post" id="addTask_form" name="addtask" enctype="multipart/form-data">
..........
<div class="form-group">
<label>File</label>
<input type="file" name="file[]" id="file" multiple />
</div>
TasksController.php
$validation = Validator::make($request->all(), [
'employee_id' => 'required',
.......
'file' => 'nullable|mimes:jpeg,png,bmp,gif,svg,docx,xlsx,pdf',
]);
if($request->hasFile('file')){
$image = $request->file('file');
$data = [];
foreach ($image as $img) {
$new_name = rand() . '.' . $img->getClientOriginalExtension();
$img->move(public_path('uploads'), $new_name);
$data[] = $new_name;
$task = Task::find($request->get('task_id'));
$task->employee_id = $request->get('employee_id');
..............
$task->file = json_encode($data);
$task->save();
}
}
Than, I need to get each value of array as a separated string in my datatable in order that it can be clickable. For example:
id | client_id | files
---|-----------|------
1 | 1 | 287319667.jpg | 2074802246.jpg
---|-----------|------
2 | 2 | 2074802246.jpg | 2074802246.jpg
Here is my datatable column:
return Datatables::of($tasks)
->editColumn('link', function ($link) {
return '<a href="public/uploads/'.$link->file.' >'.$link->file.'</a>';
})
and it returns full array like: ["287319667.jpg","2074802246.jpg"] So, I need to show like that: 287319667.jpg | 2074802246.jpg.
Thank you in advance