LARAVEL Technology & Digital Applications 5 — Questions and Answers
Question 1: Which method in a Laravel Form Request class defines the authorization logic?
- authorize() (Correct answer)
- passes()
- guard()
- policy()
Correct answer: authorize()
The `authorize()` method in a Form Request returns true or false to indicate whether the current user may make the request.
Question 2: In Laravel's task scheduling, what does `->everyFiveMinutes()` do when chained on a scheduled command?
- Runs the command every 5 seconds
- Runs the command once every 5 minutes (Correct answer)
- Delays the command start by 5 minutes
- Limits the command to run at most 5 times per hour
Correct answer: Runs the command once every 5 minutes
`->everyFiveMinutes()` schedules the command to fire every 5 minutes as evaluated by the scheduler cron.
Question 3: What is a Laravel 'macro' in the context of extending core classes?
- A Blade directive for looping
- A closure registered on a class to add custom methods at runtime (Correct answer)
- A config shortcut for frequently used settings
- A database migration template
Correct answer: A closure registered on a class to add custom methods at runtime
Macros allow you to add custom methods to Laravel classes like `Collection` or `Request` without subclassing them.
Question 4: Which Laravel Artisan command lists all registered routes with their methods, URIs, and names?
- php artisan route:show
- php artisan route:list (Correct answer)
- php artisan routes
- php artisan route:dump
Correct answer: php artisan route:list
`php artisan route:list` outputs a table of all registered routes, including their HTTP methods, URIs, middleware, and names.
Question 5: What is the purpose of the `X-CSRF-TOKEN` header in Laravel Ajax requests?
- It authenticates the user session on every Ajax call
- It protects against cross-site request forgery by verifying the token matches the session (Correct answer)
- It encrypts the request payload
- It indicates the request expects a JSON response
Correct answer: It protects against cross-site request forgery by verifying the token matches the session
Laravel's CSRF middleware verifies the `X-CSRF-TOKEN` header on state-changing requests to ensure they originate from your own app.
Question 6: In Laravel, what does calling `Storage::disk('s3')->put('file.txt', $contents)` require to be configured?
- The `filesystems.php` config with an `s3` disk entry and AWS credentials (Correct answer)
- A dedicated S3 Eloquent model
- The `php artisan storage:link` command run first
- An active Laravel Octane server
Correct answer: The `filesystems.php` config with an `s3` disk entry and AWS credentials
The `s3` disk must be defined in `config/filesystems.php` with your AWS key, secret, bucket, and region credentials.
Question 7: Which method would you use to return a paginated JSON response from an Eloquent query in a Laravel API?
- ->get()->toJson()
- ->simplePaginate()
- ->paginate() (Correct answer)
- ->chunk()
Correct answer: ->paginate()
`->paginate()` returns a `LengthAwarePaginator` which automatically serializes to JSON with `data`, `links`, and `meta` keys.
Which method in a Laravel Form Request class defines the authorization logic?