LARAVEL MCQ 3 — Questions and Answers
Question 1: What does the `withTrashed()` scope do in a Laravel Eloquent model that uses SoftDeletes?
- Permanently deletes all soft-deleted records
- Includes soft-deleted records in the query results (Correct answer)
- Restores all soft-deleted records automatically
- Prevents new soft deletes from being recorded
Correct answer: Includes soft-deleted records in the query results
`withTrashed()` removes the automatic filter that hides soft-deleted records, returning both active and soft-deleted rows.
Question 2: Which file in a Laravel application defines the service providers that are loaded on every request?
- config/app.php (Correct answer)
- bootstrap/app.php
- app/Providers/AppServiceProvider.php
- config/services.php
Correct answer: config/app.php
The `providers` array inside `config/app.php` lists all service providers Laravel registers during bootstrapping.
Question 3: What is the correct way to define an inverse one-to-many relationship in an Eloquent model?
- hasMany()
- belongsToMany()
- belongsTo() (Correct answer)
- hasOne()
Correct answer: belongsTo()
`belongsTo()` defines the inverse side of a one-to-many relationship, pointing from the child model back to its parent.
Question 4: In Laravel queues, what happens when a job exceeds its maximum number of attempts?
- It is re-queued indefinitely until manually deleted
- It is moved to the failed_jobs table (Correct answer)
- It triggers an emergency email to the admin
- It is silently discarded with no record
Correct answer: It is moved to the failed_jobs table
After exhausting its allowed attempts, a failed job is inserted into the `failed_jobs` database table for later inspection or retry.
Question 5: Which Laravel helper generates a fully-qualified URL for a named route?
- url()
- asset()
- route() (Correct answer)
- link_to_route()
Correct answer: route()
The `route('route.name', $params)` helper resolves a named route into its absolute URL including any parameters.
Question 6: What is the role of the `boot()` method in an Eloquent model?
- It initializes the database connection for the model
- It registers model events and global scopes for the model (Correct answer)
- It runs migrations for the associated table
- It seeds the table with default data
Correct answer: It registers model events and global scopes for the model
The `boot()` method is called once per model class and is the standard place to register event listeners, global scopes, and observers.
Question 7: Which Cache driver stores cached data in the application's database?
- file
- array
- database (Correct answer)
- apc
Correct answer: database
The `database` cache driver persists cached items in a SQL table (default `cache`), configured via `config/cache.php`.
What does the `withTrashed()` scope do in a Laravel Eloquent model that uses SoftDeletes?