LARAVEL Laravel 2 — Questions and Answers
Question 1: Which Artisan command creates a new middleware class in Laravel?
- php artisan make:middleware CheckAge (Correct answer)
- php artisan create:middleware CheckAge
- php artisan generate:middleware CheckAge
- php artisan new:middleware CheckAge
Correct answer: php artisan make:middleware CheckAge
The `php artisan make:middleware` command scaffolds a new middleware class in the app/Http/Middleware directory.
Question 2: In Laravel, where must a newly created middleware be registered to apply it to routes?
- app/Http/Kernel.php (Correct answer)
- config/middleware.php
- routes/web.php
- bootstrap/app.php
Correct answer: app/Http/Kernel.php
Middleware must be registered in the `$middleware`, `$middlewareGroups`, or `$routeMiddleware` arrays inside `app/Http/Kernel.php`.
Question 3: What does Laravel's `dd()` helper function do?
- Dumps the given variable and terminates script execution (Correct answer)
- Deletes a database record and logs it
- Dispatches a job and defers execution
- Defines a database driver
Correct answer: Dumps the given variable and terminates script execution
`dd()` stands for 'dump and die' — it pretty-prints variable contents using Symfony's VarDumper and then halts execution.
Question 4: Which Laravel facade provides an interface to the application's session store?
- Session (Correct answer)
- Cache
- Store
- Cookie
Correct answer: Session
The `Session` facade wraps the underlying session manager, exposing methods like `put()`, `get()`, `forget()`, and `flush()`.
Question 5: How does Laravel's Eloquent `firstOrCreate()` method differ from `firstOrNew()`?
- firstOrCreate() persists the record to the database; firstOrNew() returns an unsaved model instance (Correct answer)
- firstOrCreate() only searches; firstOrNew() always inserts
- firstOrCreate() uses raw SQL; firstOrNew() uses Eloquent
- firstOrCreate() throws an exception if not found; firstOrNew() returns null
Correct answer: firstOrCreate() persists the record to the database; firstOrNew() returns an unsaved model instance
`firstOrCreate()` saves the new model to the database immediately, while `firstOrNew()` returns a new (unsaved) model instance that you must manually call `save()` on.
Question 6: What is the purpose of Laravel's `config()` helper?
- Retrieve or set configuration values from config files at runtime (Correct answer)
- Compile and cache all configuration files
- Connect to the configuration database
- Create a new configuration file
Correct answer: Retrieve or set configuration values from config files at runtime
`config('app.timezone')` reads from config files, and `config(['app.debug' => true])` sets values at runtime using dot-notation paths.
Question 7: Which Laravel command caches all configuration files into a single file for faster loading?
- php artisan config:cache (Correct answer)
- php artisan config:compile
- php artisan optimize:config
- php artisan config:store
Correct answer: php artisan config:cache
`php artisan config:cache` merges all config files into `bootstrap/cache/config.php`, speeding up configuration access on production.
Which Artisan command creates a new middleware class in Laravel?