LARAVEL Technology & Digital Applications 4 — Questions and Answers
Question 1: Which Eloquent method is used to eager-load a relationship and apply additional constraints to it?
- load()
- with() (Correct answer)
- withCount()
- constrain()
Correct answer: with()
`with()` accepts a closure as its second argument, allowing you to constrain the eager-loaded relationship query.
Question 2: What does the `hasManyThrough` relationship in Eloquent represent?
- A model related to another through a pivot table
- A model related to a distant model through an intermediate model (Correct answer)
- A polymorphic many-to-many relationship
- A self-referential parent-child relationship
Correct answer: A model related to a distant model through an intermediate model
`hasManyThrough` provides access to distant models through an intermediate model, e.g., countries have many posts through users.
Question 3: In Laravel, which method on a job class causes it to be retried only after a delay?
- retryAfter()
- backoff() (Correct answer)
- delay()
- releaseAfter()
Correct answer: backoff()
The `backoff()` method (or property) on a job defines the number of seconds to wait before retrying a failed attempt.
Question 4: Which Laravel feature allows you to run code before and after a queued job without modifying the job class?
- Job Observers
- Job Middleware (Correct answer)
- Queue Interceptors
- Job Pipelines
Correct answer: Job Middleware
Job middleware lets you wrap additional logic around job execution—like rate limiting—without cluttering the job class itself.
Question 5: What is the purpose of the `php artisan vendor:publish` command?
- Deploys the app to a hosting vendor
- Copies package assets and config files into your application (Correct answer)
- Publishes npm packages listed in package.json
- Pushes your codebase to a Git remote
Correct answer: Copies package assets and config files into your application
`vendor:publish` copies publishable assets (configs, views, migrations) from a package into your application's directory for customization.
Question 6: Which Laravel component is used to write browser-based automated tests using a real browser?
- Laravel Pest
- Laravel Dusk (Correct answer)
- Laravel Browser
- Laravel Playwright
Correct answer: Laravel Dusk
Laravel Dusk provides an expressive API for browser automation and testing using ChromeDriver.
Question 7: What does the `sync()` method do on a many-to-many Eloquent relationship?
- Adds records without removing existing ones
- Replaces the pivot table records to match the given IDs exactly (Correct answer)
- Synchronizes the local database with a remote database
- Detaches all existing records and resets the relationship
Correct answer: Replaces the pivot table records to match the given IDs exactly
`sync()` attaches the given IDs and detaches any pivot records not in the provided list, resulting in an exact match.
Which Eloquent method is used to eager-load a relationship and apply additional constraints to it?