LARAVEL Laravel 5 — Questions and Answers
Question 1: What is the N+1 query problem in Laravel Eloquent, and how is it solved?
- Loading related models in a loop causes one query per record; solved with eager loading using with() (Correct answer)
- Running one query returns N+1 columns; solved with select()
- Using N raw queries instead of Eloquent; solved by switching to ORM
- Having more than N+1 database connections; solved by connection pooling
Correct answer: Loading related models in a loop causes one query per record; solved with eager loading using with()
Without eager loading, fetching a relationship inside a loop triggers a new query per record; `with('relation')` resolves all related models in a single additional query.
Question 2: In Laravel, what is a 'policy' used for?
- Authorizing user actions on specific Eloquent models (Correct answer)
- Defining HTTP rate-limiting rules
- Encrypting model attributes at rest
- Specifying caching strategies for model queries
Correct answer: Authorizing user actions on specific Eloquent models
Policies are classes that organize authorization logic for a model (e.g., `PostPolicy`) and are used with `$this->authorize()` or the `@can` Blade directive.
Question 3: Which Blade directive conditionally displays content only to authenticated users?
- @auth (Correct answer)
- @login
- @user
- @guard
Correct answer: @auth
`@auth` renders its content block only when a user is authenticated, while `@guest` is the inverse for unauthenticated visitors.
Question 4: What does `php artisan tinker` provide?
- An interactive REPL shell with full access to the Laravel application environment (Correct answer)
- A browser-based database management UI
- A live code linting and static analysis tool
- A command to test HTTP routes interactively
Correct answer: An interactive REPL shell with full access to the Laravel application environment
Tinker uses PsySH to provide a REPL where you can interact with your models, services, and the full application without writing a temporary script.
Question 5: In Laravel broadcasting, what is the purpose of the `ShouldBroadcast` interface on an event?
- Marks the event to be broadcast over WebSocket channels when fired (Correct answer)
- Ensures the event is queued before being dispatched
- Logs the event to all connected monitoring services
- Prevents the event from being broadcast to prevent duplicate notifications
Correct answer: Marks the event to be broadcast over WebSocket channels when fired
Implementing `ShouldBroadcast` on an event tells Laravel to broadcast the event payload to the specified channel(s) via the configured driver (Pusher, Ably, or Laravel WebSockets).
Question 6: What is the correct way to define a many-to-many relationship in Eloquent?
- return $this->belongsToMany(Role::class); (Correct answer)
- return $this->hasMany(Role::class);
- return $this->hasManyThrough(Role::class);
- return $this->morphMany(Role::class, 'roleable');
Correct answer: return $this->belongsToMany(Role::class);
`belongsToMany()` defines a many-to-many relationship using an intermediate pivot table (e.g., `role_user`) automatically derived from model names in alphabetical order.
Question 7: Which Laravel helper function generates a URL for a named route?
- route('profile', ['id' => 1]) (Correct answer)
- url('profile', ['id' => 1])
- link_to_route('profile', ['id' => 1])
- path('profile', ['id' => 1])
Correct answer: route('profile', ['id' => 1])
The `route()` helper generates a full URL for a named route, accepting the route name and an optional array of parameters to substitute into the URL.
What is the N+1 query problem in Laravel Eloquent, and how is it solved?