LARAVEL Ultimate Laravel 4 — Questions and Answers
Question 1: In Laravel's queue system, what happens to a job when its `tries` limit is exceeded?
- It is moved to the 'failed_jobs' table (Correct answer)
- It is permanently deleted from the queue
- It is re-queued indefinitely with exponential backoff
- It triggers a JobFailed event and the app crashes
Correct answer: It is moved to the 'failed_jobs' table
After all retry attempts are exhausted, Laravel moves the failed job's payload to the `failed_jobs` database table for inspection.
Question 2: Which HTTP verb does a Laravel resource route use for the `update` action?
- PUT or PATCH (Correct answer)
- POST
- GET
- DELETE
Correct answer: PUT or PATCH
Laravel resource routing maps both `PUT` and `PATCH` requests to the `update` controller action, with `PATCH` for partial updates.
Question 3: What is the purpose of Laravel Sanctum compared to Laravel Passport?
- Sanctum provides lightweight token and SPA authentication; Passport is full OAuth2 server (Correct answer)
- Sanctum handles OAuth2 flows; Passport handles simple API tokens
- Sanctum is for mobile apps only; Passport is for SPAs only
- Sanctum requires a database; Passport is stateless JWT-only
Correct answer: Sanctum provides lightweight token and SPA authentication; Passport is full OAuth2 server
Sanctum is a simple, lightweight package for SPA cookies and API tokens, while Passport implements a full OAuth2 server for third-party token issuance.
Question 4: Which Blade directive is used to escape and output a variable to prevent XSS?
- {{ $variable }} (Correct answer)
- {!! $variable !!}
- @{{ $variable }}
- {% $variable %}
Correct answer: {{ $variable }}
Double curly braces `{{ }}` automatically run `htmlspecialchars` on the value, while `{!! !!}` outputs raw unescaped HTML.
Question 5: In Laravel, what does the `DB::transaction()` method guarantee when an exception is thrown inside its callback?
- All database operations inside the callback are rolled back automatically (Correct answer)
- The exception is silently swallowed and the transaction commits
- Only the last query is rolled back, earlier queries remain
- The transaction retries the callback up to three times
Correct answer: All database operations inside the callback are rolled back automatically
If any exception is thrown inside `DB::transaction()`, Laravel automatically rolls back all queries executed within that callback.
Question 6: Which Laravel testing method asserts that a specific job was pushed onto a queue during a test?
- Queue::assertPushed(MyJob::class)
- Bus::assertDispatched(MyJob::class)
- Queue::assertPushed() and Bus::assertDispatched() both work depending on facade used (Correct answer)
- Job::assertQueued(MyJob::class)
Correct answer: Queue::assertPushed() and Bus::assertDispatched() both work depending on facade used
`Queue::assertPushed()` works after `Queue::fake()` and `Bus::assertDispatched()` works after `Bus::fake()`, both verifying job dispatch in tests.
Question 7: What is the purpose of the `RetryUntil` method (or `$retryUntil` property) on a queued job class?
- Specifies the absolute DateTime after which the job should no longer be retried (Correct answer)
- Sets the number of seconds to wait between each retry attempt
- Defines the maximum number of retry attempts for the job
- Marks the job as retryable only within a specific queue connection
Correct answer: Specifies the absolute DateTime after which the job should no longer be retried
`retryUntil()` returns a `DateTime` instance; Laravel will not retry the job after that point in time, regardless of the `tries` count.
In Laravel's queue system, what happens to a job when its `tries` limit is exceeded?