LARAVEL MCQ 2 — Questions and Answers
Question 1: Which Artisan command creates a new middleware in Laravel?
- php artisan make:filter MiddlewareName
- php artisan make:middleware MiddlewareName (Correct answer)
- php artisan create:middleware MiddlewareName
- php artisan generate:middleware MiddlewareName
Correct answer: php artisan make:middleware MiddlewareName
The `php artisan make:middleware MiddlewareName` command scaffolds a new middleware class in the `app/Http/Middleware` directory.
Question 2: In Laravel, which method on the query builder retrieves all matching records as a collection?
- first()
- find()
- get() (Correct answer)
- all()
Correct answer: get()
`get()` executes the query and returns an Eloquent Collection containing all matching rows.
Question 3: What Laravel feature allows you to define a set of routes that all share a common URI prefix?
- Route::namespace()
- Route::group() (Correct answer)
- Route::domain()
- Route::resource()
Correct answer: Route::group()
`Route::group()` (or `Route::prefix()` within a group) lets you share attributes like a URI prefix across multiple routes.
Question 4: Which Laravel class should you extend to create a custom console command?
- Illuminate\Console\Task
- Illuminate\Console\Command (Correct answer)
- Illuminate\Artisan\Command
- Illuminate\Foundation\ConsoleKernel
Correct answer: Illuminate\Console\Command
Custom Artisan commands extend `Illuminate\Console\Command`, which provides the `handle()` method and argument/option definitions.
Question 5: What is the purpose of Laravel's `dd()` helper function?
- Deletes a database record and redirects
- Dumps the given variable and terminates script execution (Correct answer)
- Dispatches a job to a queue driver
- Defines a default value for a config key
Correct answer: Dumps the given variable and terminates script execution
`dd()` stands for 'dump and die' — it outputs the variable contents and immediately stops further execution.
Question 6: Which Eloquent method would you use to update a model's attributes and save it in a single call?
- save()
- update() (Correct answer)
- fill()
- push()
Correct answer: update()
`update()` called on a query builder or model instance updates matching records in a single database statement.
Question 7: In Laravel's Blade templating, which directive is used to include a sub-view?
- @import
- @render
- @include (Correct answer)
- @embed
Correct answer: @include
`@include('view.name')` pulls in another Blade view and makes the parent's variables available to it.
Which Artisan command creates a new middleware in Laravel?