A
A
adam_carraway2018-12-20 15:03:57
Laravel
adam_carraway, 2018-12-20 15:03:57

How to upload multiple laravel images?

How to implement multiloading of images?
Now I upload one image to the server and save its path to the database:

<input type="file" name="image"  >
В контроллере:
$img = $request->file('image')->store('publick_img','public'); //загружаю на сервер
$news_body->image = $img; // сохраняю путь

Now I have set the input to multiload .
How can I accept all images in the controller, upload them to the server and save their paths to the database.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
N
netrox, 2018-12-20
@netrox

https://laraveldaily.com/upload-multiple-files-lar...

G
German Malinovsky, 2018-12-20
@fl4r3

<input type="file" name="images[]" multiple>
And now in the controller $request->file('images') will be an array that will need to be iterated.
On something like this pseudo code

$images = [];
if ($request->hasFile('images')) {
    foreach($request->file('images') as $key => $image){
      $images[] = $image->store(uniqid(), 'public');
   }
 $news_body->image = json_encode($images); 
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question