0
votes

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

1
Thanks, it works .I'm getting ["518393940.png","2009297784.png"] in my database.Besart Haziri
@Daniyal I edited my question about getting array value. Could you please help me?Besart Haziri
Yes, I tried to implode but it returns full array(["287319667.jpg","2074802246.jpg"]) My Code: $files[] = $link->file; return '<a href="public/uploads/'.$files.' >'.implode(' ', $files).'</a>';Besart Haziri

1 Answers

0
votes

What you're doing will work for single file if you want to validate multiple file use this

'file.*' => 'required|file|mimes:xlsx,xls,csv,jpg,jpeg,****'