LARAVEL Technology & Digital Applications 2 β Questions and Answers
Question 1: Which Laravel feature allows you to define reusable query logic that can be chained onto Eloquent queries?
- Query Macros
- Local Scopes (Correct answer)
- Global Observers
- Query Bindings
Correct answer: Local Scopes
Local scopes let you define reusable constraint methods on a model that are chained using a `scope` prefix.
Question 2: What is the purpose of Laravel's `php artisan optimize` command?
- Minifies JavaScript and CSS assets
- Caches config, routes, and views for faster bootstrapping (Correct answer)
- Runs database query optimization
- Compiles Blade templates to PHP
Correct answer: Caches config, routes, and views for faster bootstrapping
`php artisan optimize` caches configuration, routes, and views so the application bootstraps faster in production.
Question 3: Which Laravel component handles broadcasting events over WebSockets to frontend clients?
- Laravel Horizon
- Laravel Echo (Correct answer)
- Laravel Dusk
- Laravel Octane
Correct answer: Laravel Echo
Laravel Echo is the JavaScript library that subscribes to broadcast channels and listens for server-side events.
Question 4: What does the `withTrashed()` method do in Eloquent?
- Permanently deletes soft-deleted records
- Includes soft-deleted records in query results (Correct answer)
- Restores the most recently soft-deleted record
- Prevents records from being soft-deleted
Correct answer: Includes soft-deleted records in query results
`withTrashed()` includes soft-deleted (where `deleted_at` is not null) records in the query results.
Question 5: In Laravel, which HTTP method is used by a route defined with `Route::patch()`?
- POST
- PUT
- PATCH (Correct answer)
- DELETE
Correct answer: PATCH
`Route::patch()` registers a route that responds only to HTTP PATCH requests, used for partial resource updates.
Question 6: Which tool does Laravel Sail use under the hood to manage the development environment?
- Vagrant
- VirtualBox
- Docker (Correct answer)
- Kubernetes
Correct answer: Docker
Laravel Sail is a lightweight CLI for interacting with Laravel's default Docker development environment.
Question 7: What is the correct way to pass data to all views in a Laravel application from a service provider?
- Using `View::composer()`
- Using `View::share()` (Correct answer)
- Using `View::make()`
- Using `View::creator()`
Correct answer: Using `View::share()`
`View::share()` makes a variable available to every view rendered by the application.
Which Laravel feature allows you to define reusable query logic that can be chained onto Eloquent queries?