Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to validate a file upload in a Laravel controller.
Laravel
request()->validate(['photo' => '[1]']);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'string' or 'email' rules for file uploads.
Not including 'file' in the validation rules.
✗ Incorrect
The validation rule 'required|file|image' ensures the uploaded file is present and is an image.
2fill in blank
mediumComplete the code to store an uploaded file in Laravel's storage.
Laravel
$path = $request->file('photo')->[1]('photos');
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'move' which is a PHP method but not recommended in Laravel.
Using 'upload' which does not exist.
✗ Incorrect
The 'store' method saves the uploaded file to the specified disk folder.
3fill in blank
hardFix the error in the code to get the original filename of the uploaded file.
Laravel
$filename = $request->file('photo')->[1]();
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'getOriginalName' which does not exist.
Using 'originalName' property which is invalid.
✗ Incorrect
The correct method to get the original filename is 'getClientOriginalName()'.
4fill in blank
hardFill both blanks to check if a file was uploaded and then store it.
Laravel
if ($request->hasFile('[1]') && $request->file('[2]')->isValid()) { $path = $request->file('photo')->store('uploads'); }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names in 'hasFile' and 'file' methods.
Using generic names like 'file' or 'upload' instead of the actual input name.
✗ Incorrect
The 'hasFile' and 'file' methods must use the same input name 'photo' to check and store the file.
5fill in blank
hardFill all three blanks to create a unique filename and store the uploaded file.
Laravel
$filename = time() . '_' . $request->file('[1]')->getClientOriginalName(); $path = $request->file('[2]')->storeAs('[3]', $filename);
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different input names in the two file calls.
Using wrong folder names for storage.
✗ Incorrect
Use the input name 'photo' for both file calls and store the file in the 'uploads' folder.