21 questions / 10 random questions
Random questions, instant feedback, and review for missed questions.
In Laravel, you want GET /users/{user} to call UserController@show. Which route definition is most natural?
Answer: Route::get('/users/{user}', [UserController::class, 'show']);
Route::get defines a route for GET requests. The array form can specify the controller class and method.
In Laravel, you want to receive a User model automatically from the {user} route parameter. Which feature is mainly used?
Answer: Route model binding
Route model binding can inject the matching Eloquent model into a controller argument from a route parameter.
In a Laravel controller, what is preferable to reading request input directly from $_POST?
Answer: Receive a Request object, validate it, and use it
Laravel's Request object lets you handle input, authorization, and validation using framework conventions.
Input validation is complex and reused by multiple controllers. Where is it natural to move it in Laravel?
Answer: Form Request class
A Form Request can move authorization and validation rules into a dedicated class, helping keep controllers thin.
In Laravel, for a one-to-many relationship between users and posts, which relation is typically defined on the User model?
Answer: hasMany(Post::class)
Because a user has many posts, the User model defines hasMany. From the post side back to the user, belongsTo is used.
A list page displays posts and authors and suffers from N+1 queries. What should you consider first in Laravel?
Answer: Use eager loading with with('user')
Eager loading preloads related models and reduces additional queries inside loops.
In Laravel, which model property is commonly used to handle mass assignment safely?
Answer: $fillable
$fillable explicitly lists attributes allowed for mass assignment. It helps prevent unintended column updates.
In Laravel, what is used to share database schema changes across a team and manage them as history?
Answer: Migration
Migrations manage database schema changes such as creating tables or adding columns as code.
Why does migration order matter when dropping tables with foreign key constraints?
Answer: Dropping a referenced table first can violate constraints
With foreign key constraints, you must plan the order, such as dropping constraints or child tables first.
In Laravel, you want to validate that email is required and has email format. Which rule set is representative?
Answer: 'email' => ['required', 'email']
required checks presence, and email checks email format. Complex inputs are often managed in a Form Request.
When validation fails in Laravel, what is the usual behavior for a normal web request?
Answer: Redirect back and flash error information to the session
For web requests, validation failure commonly redirects back and makes errors and old input available for display.
In Laravel, you want to restrict a route to authenticated users. What is commonly used?
Answer: auth middleware
The auth middleware protects routes that require authentication, such as redirecting guests to a login page.
In Laravel, where is CSRF protection mainly needed?
Answer: State-changing form submissions and POST/PUT/DELETE requests
CSRF abuses a user's authenticated state to cause unintended state changes. Token verification matters for state-changing requests.
You want to defer heavy work such as sending mail or processing images so the HTTP response is not delayed. What Laravel feature is commonly used?
Answer: Queue Job
Queue Jobs let you dispatch heavy work to a queue and process it asynchronously with workers.
Why is it often necessary to restart Laravel queue workers after deployment?
Answer: Workers are long-running and may not automatically reload new code
Queue workers are long-running processes, so deployment often requires restarting them to load new code or configuration.
Why is Blade's normal {{ $name }} output considered safer for displaying user input in HTML?
Answer: Because it outputs HTML-escaped content
Blade's {{ }} output is escaped by default. Raw output with {!! !!} should be used carefully.
You want to reuse a shared header or form part across multiple screens. What is a good Blade candidate?
Answer: Blade component
Blade components can reuse view parts and reduce template duplication.
In Laravel, where is it appropriate to organize logic that decides whether the logged-in user can update a specific post?
Answer: Policy
Policies organize authorization logic for models and reduce scattered checks in controllers or Blade views.
You want to get the authenticated user's information in a Laravel controller. Which method is representative?
Answer: $request->user()
$request->user() is a common way to get the currently authenticated user. Authorization can be combined with policies or gates.
In Laravel production, .env was changed but configuration did not update. What should you suspect first?
Answer: config cache
Production often uses config:cache. If configuration is cached, the cache must be regenerated after .env changes.
In Laravel, you want secrets to vary by environment without hard-coding them. What is the basic approach?
Answer: Use .env or external secret management and read through config
Secrets should be separated from source code and supplied through environment variables or secret management into configuration.