LARAVEL Case Studies & Practical Application 3 — Questions and Answers
Question 1: A financial app must log every change to an Account model (old values, new values, user who changed it). What is the most maintainable Laravel approach?
- Create an AuditLog model and record changes inside an Eloquent observer's updating and deleting hooks (Correct answer)
- Add logging code inside every controller method that touches accounts
- Use a database trigger to insert audit rows on UPDATE
- Store a JSON snapshot of every model in the session after each save
Correct answer: Create an AuditLog model and record changes inside an Eloquent observer's updating and deleting hooks
Eloquent observers centralize audit logic outside controllers and fire automatically on model lifecycle events.
Question 2: You are building a reporting dashboard that runs 5 complex SQL aggregations, each taking 2–4 seconds. Users refresh frequently. What caching strategy works best with Laravel?
- Cache each query result with Cache::remember() using a meaningful key and a TTL like 300 seconds (Correct answer)
- Run all queries at midnight using a scheduled command and store results in a reports table
- Use DB::statement('SET SESSION query_cache_type=1') to enable MySQL query cache
- Wrap all 5 queries in a transaction to speed them up
Correct answer: Cache each query result with Cache::remember() using a meaningful key and a TTL like 300 seconds
Cache::remember() returns cached data on subsequent calls and only re-runs the query when the TTL expires.
Question 3: A legacy Laravel 8 app uses string-based middleware like 'auth'. You are upgrading to Laravel 11, which requires class-based middleware. What file defines the global middleware stack in Laravel 11?
- bootstrap/app.php using withMiddleware() (Correct answer)
- app/Http/Kernel.php in the $middleware array
- config/middleware.php
- routes/web.php using Route::middleware()
Correct answer: bootstrap/app.php using withMiddleware()
Laravel 11 removed Kernel.php and consolidates HTTP configuration, including middleware, into bootstrap/app.php.
Question 4: A public blog needs to prevent the same IP from submitting more than 5 comments per minute. Which built-in Laravel feature handles this with minimal code?
- Rate limiting via RateLimiter::for() in a service provider and applying the throttle middleware to the comment route (Correct answer)
- A manual counter stored in the session and checked in the controller
- A CRON job that deletes excess comments from the database
- IP blocking at the Nginx level via deny directives
Correct answer: Rate limiting via RateLimiter::for() in a service provider and applying the throttle middleware to the comment route
Laravel's throttle middleware backed by RateLimiter::for() provides named, configurable rate limits per route or route group.
Question 5: You need to store user profile photos securely so that each file URL expires after 15 minutes. Which Laravel Storage method generates a temporary signed URL?
- Storage::temporaryUrl($path, now()->addMinutes(15)) (Correct answer)
- Storage::url($path, ['expires' => 900])
- Storage::signedUrl($path, 15)
- Storage::disk('s3')->privateUrl($path)
Correct answer: Storage::temporaryUrl($path, now()->addMinutes(15))
temporaryUrl() is the Storage facade method that generates a pre-signed, time-limited URL supported by drivers like S3.
Question 6: A team's feature branch introduced a new payments table. A junior developer ran php artisan migrate on production without reviewing it first. What mechanism should have prevented this?
- The --force flag requirement in production and a CI/CD gate that requires approval before running migrations (Correct answer)
- Storing migrations in a separate Git repository
- Disabling the migrate command in production via a custom Artisan command
- Using database seeders instead of migrations for schema changes
Correct answer: The --force flag requirement in production and a CI/CD gate that requires approval before running migrations
Laravel requires --force in production environments to prevent accidental migration runs, and CI/CD gates add a human approval step.
Question 7: Your Laravel app sends transactional emails via SMTP. During a load test, email sending adds 1.5 seconds to each request. What is the fix?
- Use Mail::queue() to dispatch email sending to a background queue worker instead of Mail::send() (Correct answer)
- Increase server memory to reduce SMTP connection time
- Switch to a synchronous faster SMTP provider
- Store emails in a database table and send them via a cron job every 5 minutes
Correct answer: Use Mail::queue() to dispatch email sending to a background queue worker instead of Mail::send()
Mail::queue() places the mailable on the queue so the HTTP response returns immediately while a worker sends the email asynchronously.
A financial app must log every change to an Account model (old values, new values, user who changed it).
What is the most maintainable Laravel approach?