LARAVEL Ultimate Laravel 5 — Questions and Answers
Question 1: Which Laravel feature allows you to define complex database search queries using a fluent, reusable object instead of scattering `where` clauses across your codebase?
- Eloquent Query Scopes (Correct answer)
- Database Factories
- Query Macros
- Model Observers
Correct answer: Eloquent Query Scopes
Eloquent local and global scopes let you encapsulate reusable query constraints as methods on the model, callable in a fluent chain.
Question 2: In Laravel, what is the purpose of the `php artisan tinker` command?
- Opens an interactive REPL to execute PHP code within the full Laravel application context (Correct answer)
- Runs all unit and feature tests interactively
- Provides a browser-based database GUI
- Starts a live-reload development server
Correct answer: Opens an interactive REPL to execute PHP code within the full Laravel application context
Tinker uses PsySH to provide a REPL where you can interact with your models, services, and any application code directly.
Question 3: What does `Event::fake()` do when called in a Laravel test?
- Prevents real event listeners from firing and allows assertions on dispatched events (Correct answer)
- Fires all events synchronously in the same process
- Replaces all listeners with logging stubs that record calls
- Disables the event system entirely for all subsequent tests
Correct answer: Prevents real event listeners from firing and allows assertions on dispatched events
`Event::fake()` intercepts event dispatching so listeners never fire, then exposes `assertDispatched()` and `assertNotDispatched()` for assertions.
Question 4: Which configuration file in Laravel controls the default timezone used for date and time operations?
- config/app.php (Correct answer)
- config/database.php
- config/session.php
- .env APP_TIMEZONE
Correct answer: config/app.php
The `timezone` key in `config/app.php` sets the default PHP timezone used for Carbon dates and all date-related operations in the app.
Question 5: In Laravel, what is the difference between `firstOrCreate()` and `firstOrNew()` on an Eloquent model?
- firstOrCreate() saves to the database; firstOrNew() returns an unsaved model instance (Correct answer)
- firstOrNew() saves to the database; firstOrCreate() only returns in memory
- Both save to the database but firstOrNew() uses upsert logic
- firstOrCreate() triggers events; firstOrNew() bypasses all model events
Correct answer: firstOrCreate() saves to the database; firstOrNew() returns an unsaved model instance
`firstOrCreate()` finds or inserts the record into the database, while `firstOrNew()` finds or returns a new unsaved model instance.
Question 6: Which method do you call on an Eloquent relationship to eager load it and prevent the N+1 query problem?
- with() (Correct answer)
- load()
- eager()
- join()
Correct answer: with()
Calling `->with('relation')` on a query eager loads the relationship in a second query, avoiding a separate query per model instance.
Question 7: What does the `php artisan route:list` command display?
- All registered routes with their HTTP methods, URIs, names, and controller actions (Correct answer)
- Only the named routes defined in the application
- Routes that are currently cached and in use
- All middleware registered with each route group
Correct answer: All registered routes with their HTTP methods, URIs, names, and controller actions
`route:list` outputs a table of every registered route including its method, URI, name, action, and assigned middleware.
Which Laravel feature allows you to define complex database search queries using a fluent, reusable object instead of scattering `where` clauses across your codebase?