LARAVEL Research & Evidence-Based Practice 5 — Questions and Answers
Question 1: Which evidence-based approach does Laravel recommend to prevent SQL injection when using raw expressions in the query builder?
- Escape strings with htmlspecialchars()
- Use PDO::quote() on all inputs
- Use parameter bindings with ? or named placeholders (Correct answer)
- Wrap values in DB::raw() calls
Correct answer: Use parameter bindings with ? or named placeholders
Laravel's query builder uses PDO parameter binding for all user input, which prevents SQL injection at the driver level.
Question 2: Which contract should you type-hint to decouple a class from a specific cache implementation, supporting evidence-based dependency inversion?
- Illuminate\Cache\CacheManager
- Illuminate\Contracts\Cache\Repository (Correct answer)
- Illuminate\Support\Facades\Cache
- Psr\SimpleCache\CacheInterface
Correct answer: Illuminate\Contracts\Cache\Repository
Type-hinting Illuminate\Contracts\Cache\Repository decouples the class from any specific cache driver via the service container.
Question 3: When research reveals excessive memory usage in a large data export, which Eloquent method processes records in smaller batches to reduce memory footprint?
- paginate()
- lazy()
- cursor()
- chunk() (Correct answer)
Correct answer: chunk()
chunk() retrieves records in batches and passes each batch to a closure, keeping only one chunk in memory at a time.
Question 4: Which Laravel route registration method reduces boilerplate by automatically generating five RESTful API routes from a single line?
- Route::resource()
- Route::controller()
- Route::group()
- Route::apiResource() (Correct answer)
Correct answer: Route::apiResource()
Route::apiResource() registers five RESTful routes (index, store, show, update, destroy) without the HTML form routes.
Question 5: When comparing authentication packages for a new project, which two packages does Laravel officially maintain for different authentication use cases?
- Passport only
- Sanctum only
- Both Sanctum and Passport are officially maintained (Correct answer)
- JWT-auth by tymon
Correct answer: Both Sanctum and Passport are officially maintained
Laravel maintains both Sanctum (lightweight SPA/mobile tokens) and Passport (full OAuth2 server) for different use cases.
Question 6: Which artisan command clears the config, route, view, and event caches simultaneously, ensuring a service provider update takes full effect?
- php artisan optimize:clear (Correct answer)
- php artisan cache:clear
- php artisan config:cache
- php artisan provider:reload
Correct answer: php artisan optimize:clear
`php artisan optimize:clear` clears the config, route, view, and event caches, forcing fresh resolution of all bindings.
Question 7: Which Laravel testing assertion verifies that a Mailable was sent to a specific email address during a test after calling Mail::fake()?
- Mail::assertSentTo() (Correct answer)
- Mail::assertDelivered()
- Mail::assertQueued()
- Mail::assertReceived()
Correct answer: Mail::assertSentTo()
Mail::assertSentTo() checks that a specific Mailable class was sent to the given recipient after faking the mailer.
Which evidence-based approach does Laravel recommend to prevent SQL injection when using raw expressions in the query builder?