LARAVEL Ultimate Laravel 3 — Questions and Answers
Question 1: Which Laravel command is used to run only the most recently created migration file?
- php artisan migrate --step=1 (Correct answer)
- php artisan migrate:last
- php artisan migrate:single
- php artisan migrate --latest
Correct answer: php artisan migrate --step=1
`php artisan migrate --step=1` runs exactly one pending migration, which is the most recently created one.
Question 2: In Laravel Broadcasting, what interface must a model implement to broadcast its events on a private channel scoped to that model?
- Illuminate\Contracts\Broadcasting\HasBroadcastChannel (Correct answer)
- Illuminate\Broadcasting\PrivateChannel
- Illuminate\Contracts\Auth\Authenticatable
- Illuminate\Broadcasting\BroadcastServiceProvider
Correct answer: Illuminate\Contracts\Broadcasting\HasBroadcastChannel
Implementing `HasBroadcastChannel` and defining `broadcastChannel()` allows Eloquent models to expose a named private channel.
Question 3: Which Eloquent relationship method would you use to define a 'belongs to many' with an intermediate pivot table?
- belongsToMany() (Correct answer)
- hasManyThrough()
- morphToMany()
- hasMany()
Correct answer: belongsToMany()
`belongsToMany()` defines a many-to-many relationship and expects a pivot table named by combining the two model names alphabetically.
Question 4: What is the default behavior of Laravel's `Route::resource()` when the model cannot be found during route model binding?
- It returns a 404 HTTP response (Correct answer)
- It creates a new model instance and continues
- It throws an unhandled ModelNotFoundException
- It redirects to the index route
Correct answer: It returns a 404 HTTP response
By default, route model binding automatically returns a 404 response when the model is not found in the database.
Question 5: Which method allows you to add a global scope to an Eloquent model so it applies to every query on that model?
- boot() with addGlobalScope() (Correct answer)
- static::creating() callback
- query() override
- static::booted() with observe()
Correct answer: boot() with addGlobalScope()
Defining `addGlobalScope()` inside the model's `boot()` method registers a scope class or closure that applies to all queries.
Question 6: In Laravel, what is the role of a 'Policy' class?
- Centralizes authorization logic for a specific model or resource (Correct answer)
- Defines HTTP middleware rules for route groups
- Validates incoming request data for a resource
- Stores configuration values per domain
Correct answer: Centralizes authorization logic for a specific model or resource
Policy classes group authorization logic (e.g., `view`, `create`, `update`, `delete`) for a given model and are registered with the Gate.
Question 7: What does calling `php artisan optimize:clear` do in a Laravel application?
- Clears all compiled/cached files including config, routes, views, and events (Correct answer)
- Removes all files from the `storage/app` directory
- Drops and recreates the database cache tables
- Deletes all queue job records from the database
Correct answer: Clears all compiled/cached files including config, routes, views, and events
`optimize:clear` runs all the individual `cache:clear`, `config:clear`, `route:clear`, `view:clear`, and `event:clear` commands at once.
Which Laravel command is used to run only the most recently created migration file?