LARAVEL Case Studies & Practical Application 5 — Questions and Answers
Question 1: A Laravel app needs to run a database-heavy report every night at 2 AM without overlapping runs if the previous job is still executing. How do you configure this in the scheduler?
- Use ->dailyAt('02:00')->withoutOverlapping() on the scheduled command (Correct answer)
- Use ->cron('0 2 * * *') and add a pid file check in the command itself
- Set QUEUE_TIMEOUT=86400 in .env and dispatch the job at midnight
- Use ->everyMinute()->between('02:00', '02:01')
Correct answer: Use ->dailyAt('02:00')->withoutOverlapping() on the scheduled command
withoutOverlapping() acquires a mutex lock so the scheduler skips the next run if the previous execution is still in progress.
Question 2: A React SPA fetches data from a Laravel API on a different subdomain. The browser blocks requests with a CORS error. Where do you configure allowed origins in Laravel?
- In config/cors.php by setting the allowed_origins array and ensuring the HandleCors middleware is active (Correct answer)
- In routes/api.php by adding a header('Access-Control-Allow-Origin', '*') call
- In .htaccess with a Header set directive
- In the API controller's constructor using response()->header()
Correct answer: In config/cors.php by setting the allowed_origins array and ensuring the HandleCors middleware is active
config/cors.php is Laravel's dedicated CORS configuration file, consumed by the bundled HandleCors middleware.
Question 3: You are profiling a slow Laravel page and suspect inefficient queries. Which built-in tool shows every query executed during a request along with its duration?
- Laravel Debugbar or DB::listen() logging each query in a local environment
- php artisan query:log --request
- config/database.php slow_query_threshold setting
- The Laravel Telescope query watcher in the local environment (Correct answer)
Correct answer: The Laravel Telescope query watcher in the local environment
Laravel Telescope's query watcher records every SQL query, its bindings, and execution time for requests in the local or staging environment.
Question 4: A user-uploaded CSV is processed by a job that creates thousands of Eloquent models. Memory usage spikes and the process is killed. What Eloquent technique reduces memory consumption?
- Use cursor() or lazy() to process rows one at a time instead of loading the full collection into memory (Correct answer)
- Increase PHP memory_limit to 2GB in php.ini
- Use chunk() only for reads and insert all rows in a single Eloquent::create() loop
- Disable Eloquent events during the import with Model::unguard()
Correct answer: Use cursor() or lazy() to process rows one at a time instead of loading the full collection into memory
cursor() uses a PHP generator to yield one model at a time, keeping memory usage flat regardless of dataset size.
Question 5: A team is building a microservices architecture where a Laravel service must react to events published by a Node.js service via RabbitMQ. What is the recommended approach?
- Use a custom queue driver or package (e.g., vladimir-yuldashev/laravel-queue-rabbitmq) to connect Laravel's queue system to RabbitMQ (Correct answer)
- Poll the RabbitMQ HTTP API every second from a Laravel scheduled command
- Have the Node.js service POST directly to a Laravel webhook endpoint instead
- Use Laravel Echo with Socket.io to receive RabbitMQ messages in real time
Correct answer: Use a custom queue driver or package (e.g., vladimir-yuldashev/laravel-queue-rabbitmq) to connect Laravel's queue system to RabbitMQ
A RabbitMQ queue driver integrates natively with Laravel's queue workers, allowing jobs to be consumed from RabbitMQ queues using standard artisan queue:work.
Question 6: An artisan command seeds the database for local development but must never run in production. What is the safest enforcement mechanism?
- Check app()->environment('production') at the start of the command and exit with an error if true (Correct answer)
- Remove the seeder class from the production server's file system
- Gate the command behind a .env variable ALLOW_SEED=true and document it
- Rely on developers to remember not to run it in production
Correct answer: Check app()->environment('production') at the start of the command and exit with an error if true
An environment check inside the command's handle() method provides a code-level guard that fires regardless of who or what invokes the command.
Question 7: A Laravel application under heavy load shows high database connection usage. The ops team asks you to limit simultaneous connections without changing MySQL max_connections. What Laravel config controls the application-side connection pool size?
- The options array in config/database.php, specifically PDO::ATTR_PERSISTENT combined with server-side pooling via PgBouncer or ProxySQL (Correct answer)
- Setting DB_MAX_CONNECTIONS in the .env file
- The max_connections key directly in config/database.php
- Reducing QUEUE_WORKER_COUNT to limit concurrent workers
Correct answer: The options array in config/database.php, specifically PDO::ATTR_PERSISTENT combined with server-side pooling via PgBouncer or ProxySQL
PHP-FPM is stateless so true connection pooling requires a proxy like ProxySQL; Laravel's config/database.php PDO options configure per-connection behavior.
A Laravel app needs to run a database-heavy report every night at 2 AM without overlapping runs if the previous job is still executing.
How do you configure this in the scheduler?