LARAVEL MCQ 4 — Questions and Answers
Question 1: In Laravel, which method chains a `WHERE` clause using an OR logical operator?
- orWhere() (Correct answer)
- whereOr()
- orFilter()
- whereElse()
Correct answer: orWhere()
`orWhere()` appends an `OR WHERE` condition to the query builder chain.
Question 2: What does the `php artisan migrate:rollback` command do?
- Rolls back all migrations ever run
- Rolls back the last batch of migrations (Correct answer)
- Deletes the migrations table entirely
- Re-runs the most recent migration
Correct answer: Rolls back the last batch of migrations
`migrate:rollback` reverts only the migrations belonging to the last batch, incrementing the batch counter in reverse.
Question 3: Which of the following is the correct way to pass data from a controller to a Blade view?
- return view('name')->attach(['key' => $value])
- return view('name', ['key' => $value]) (Correct answer)
- return view('name')->send(['key' => $value])
- return view('name')->push(['key' => $value])
Correct answer: return view('name', ['key' => $value])
Passing an associative array as the second argument to `view()` makes each key available as a variable inside the Blade template.
Question 4: What is Laravel Passport primarily used for?
- Managing user roles and permissions
- Full OAuth2 server implementation for API authentication (Correct answer)
- Sending transactional emails
- Scheduling recurring console commands
Correct answer: Full OAuth2 server implementation for API authentication
Laravel Passport provides a complete OAuth2 server so your application can issue and validate API access tokens.
Question 5: Which Blade directive outputs escaped (HTML-safe) content?
- {!! $var !!}
- {{ $var }} (Correct answer)
- @raw($var)
- @echo($var)
Correct answer: {{ $var }}
`{{ $var }}` runs the value through `htmlspecialchars()`, preventing XSS, whereas `{!! !!}` outputs raw unescaped HTML.
Question 6: In Laravel's event system, what is a Listener?
- A class that fires an event and broadcasts it to subscribers
- A class that handles the logic when a specific event is dispatched (Correct answer)
- A middleware that intercepts HTTP requests
- A job that runs asynchronously on a queue
Correct answer: A class that handles the logic when a specific event is dispatched
A Listener is a class with a `handle()` method that executes business logic in response to a specific fired Event.
Question 7: Which method would you use on a relationship to eager-load it and prevent N+1 queries?
- load()
- with() (Correct answer)
- join()
- attach()
Correct answer: with()
`with('relationship')` tells Eloquent to eager-load the specified relationship in the initial query, eliminating N+1 query problems.
In Laravel, which method chains a `WHERE` clause using an OR logical operator?