LARAVEL Ultimate Laravel 2 — Questions and Answers
Question 1: Which Artisan command creates a new middleware class in Laravel?
- php artisan make:middleware MyMiddleware (Correct answer)
- php artisan create:middleware MyMiddleware
- php artisan generate:middleware MyMiddleware
- php artisan new:middleware MyMiddleware
Correct answer: php artisan make:middleware MyMiddleware
The `php artisan make:middleware` command scaffolds a new middleware class in the `app/Http/Middleware` directory.
Question 2: In Laravel, what does the `withTrashed()` scope do when querying an Eloquent model?
- Includes soft-deleted records in query results (Correct answer)
- Permanently deletes all matching records
- Restores all soft-deleted records
- Excludes soft-deleted records from results
Correct answer: Includes soft-deleted records in query results
`withTrashed()` extends a query to include rows where `deleted_at` is not null, used with soft-deleting models.
Question 3: Which Laravel facade is used to dispatch a queued job?
- Queue::push()
- Bus::dispatch() (Correct answer)
- Job::queue()
- Dispatch::job()
Correct answer: Bus::dispatch()
The `Bus` facade (or the `dispatch()` helper) dispatches jobs; `Bus::dispatch(new MyJob())` queues or executes the job.
Question 4: What is the purpose of Laravel's `config/cache.php` `prefix` option?
- Prepends a string to all cache keys to avoid collisions across applications (Correct answer)
- Sets the default cache driver name
- Defines the maximum size of cached values
- Specifies the cache serialization format
Correct answer: Prepends a string to all cache keys to avoid collisions across applications
The `prefix` option is prepended to every cache key, preventing collisions when multiple apps share the same cache store.
Question 5: Which method on a Laravel Collection lazily evaluates a chain of operations without loading all items into memory at once?
- lazy() (Correct answer)
- cursor()
- chunk()
- stream()
Correct answer: lazy()
`LazyCollection::lazy()` or using `LazyCollection::make()` evaluates items one at a time using PHP generators, minimizing memory usage.
Question 6: In Laravel's service container, what is 'contextual binding' used for?
- Injecting different implementations of an interface depending on which class requests it (Correct answer)
- Binding a concrete class only during testing
- Providing default values for optional constructor parameters
- Resolving services only after the application has fully booted
Correct answer: Injecting different implementations of an interface depending on which class requests it
Contextual binding lets you specify that when class A needs an interface, it gets implementation X, while class B gets implementation Y.
Question 7: What does the `@stack` Blade directive do?
- Renders content that was pushed onto a named stack from child views (Correct answer)
- Defines a new layout section for child templates
- Pushes JavaScript assets to the page footer
- Stacks multiple Blade components in a vertical layout
Correct answer: Renders content that was pushed onto a named stack from child views
`@stack('name')` outputs everything pushed to that named stack via `@push` directives in child or included views.
Which Artisan command creates a new middleware class in Laravel?