Bird
0
0

You want to upload multiple images and save their paths in the database. Which Laravel approach correctly handles multiple file uploads in a request named photos?

hard📝 Application Q15 of 15
Laravel - Request and Response
You want to upload multiple images and save their paths in the database. Which Laravel approach correctly handles multiple file uploads in a request named photos?
A<pre>if ($request->hasFile('photos')) { $path = $request->file('photos')->store('gallery'); Photo::create(['path' => $path]); }</pre>
B<pre>$photos = $request->file('photos'); $photos->store('gallery'); Photo::create(['path' => $photos]);</pre>
C<pre>foreach ($request->file('photos') as $photo) { $path = $photo->store('gallery'); Photo::create(['path' => $path]); }</pre>
D<pre>$path = $request->file('photos')->move('gallery'); Photo::create(['path' => $path]);</pre>
Step-by-Step Solution
Solution:
  1. Step 1: Understand multiple file upload handling

    Multiple files come as an array; you must loop through each file to store and save paths.
  2. Step 2: Analyze options for correct looping and storing

    foreach ($request->file('photos') as $photo) {
      $path = $photo->store('gallery');
      Photo::create(['path' => $path]);
    }
    loops over each file, stores it, and saves path correctly. Others treat files as single or misuse methods.
  3. Final Answer:

    Loop through each file, store, and save path -> Option C
  4. Quick Check:

    Multiple files need foreach loop = A [OK]
Quick Trick: Use foreach to handle multiple uploaded files individually [OK]
Common Mistakes:
  • Treating multiple files as single file
  • Not looping through file array
  • Calling store() on file array directly

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Laravel Quizzes