LARAVEL MCQ 5 — Questions and Answers
Question 1: What does the `$fillable` property control in an Eloquent model?
- The columns excluded from JSON serialization
- The columns allowed for mass assignment (Correct answer)
- The default values for each database column
- The columns used to uniquely identify a record
Correct answer: The columns allowed for mass assignment
`$fillable` is a whitelist of column names that may be set via `create()` or `fill()`, protecting against mass-assignment vulnerabilities.
Question 2: Which Laravel contract must a Mailable class implement (or extend) to be sent via `Mail::send()`?
- Illuminate\Mail\Contracts\Mailer
- Illuminate\Contracts\Queue\ShouldQueue
- Illuminate\Mail\Mailable (Correct answer)
- Illuminate\Notifications\Notification
Correct answer: Illuminate\Mail\Mailable
Mailable classes extend `Illuminate\Mail\Mailable` and define a `build()` method that configures the email's view, subject, and attachments.
Question 3: What is the purpose of Laravel's `tinker` command?
- Runs unit tests in interactive mode
- Opens a REPL to interact with your application in real time (Correct answer)
- Compiles and minifies frontend assets
- Monitors the queue and retries failed jobs
Correct answer: Opens a REPL to interact with your application in real time
`php artisan tinker` launches a PsySH REPL shell where you can execute PHP and Eloquent queries against your live application.
Question 4: In Laravel, what does the `sync()` method do on a many-to-many relationship?
- Inserts new pivot records without removing existing ones
- Replaces all existing pivot records with the provided IDs (Correct answer)
- Synchronizes the pivot table with the main table schema
- Updates timestamps on all related pivot rows
Correct answer: Replaces all existing pivot records with the provided IDs
`sync()` detaches any pivot records not in the provided array and attaches any new ones, leaving only the specified relationships.
Question 5: Which HTTP verb does a Laravel `Route::patch()` route respond to?
- POST
- PUT
- PATCH (Correct answer)
- DELETE
Correct answer: PATCH
`Route::patch()` registers a route that only accepts HTTP PATCH requests, typically used for partial resource updates.
Question 6: What is the purpose of the `AppServiceProvider::register()` method?
- Bootstrapping event listeners and middleware
- Binding interfaces to concrete implementations in the service container (Correct answer)
- Running database seeders on application start
- Defining route model binding for the application
Correct answer: Binding interfaces to concrete implementations in the service container
The `register()` method is for binding classes or interfaces into the IoC container; event listeners and other bootstrapping belong in `boot()`.
Question 7: Which Laravel feature allows you to automatically inject dependencies into a class constructor based on type hints?
- Facades
- Dependency Injection via the Service Container (Correct answer)
- Middleware chaining
- Model Observers
Correct answer: Dependency Injection via the Service Container
Laravel's IoC service container automatically resolves and injects type-hinted dependencies into constructors and controller methods.
What does the `$fillable` property control in an Eloquent model?