Sure! Here are some advanced-level Laravel quiz questions with answers:
How do you define a global scope in Laravel's Eloquent ORM?
- Answer: You can define a global scope by creating a class that implements the
Illuminate\Database\Eloquent\Scope
interface, then applying it using theaddGlobalScope
method in the model'sboot
method.
- Answer: You can define a global scope by creating a class that implements the
What is the purpose of the
AppServiceProvider
class in Laravel?- Answer: The
AppServiceProvider
is one of the default service providers in a Laravel application. It is used to bind services and perform initial setup when the application boots. You can register application-wide services and bindings here.
- Answer: The
Explain the difference between
Queue::push
andQueue::later
in Laravel.- Answer:
Queue::push
dispatches a job to the queue to be executed immediately, whileQueue::later
schedules the job to be executed after a specified delay.
- Answer:
What is the
LazyCollection
class in Laravel, and when would you use it?- Answer:
LazyCollection
allows you to work with large data sets while using minimal memory by leveraging PHP generators. It's useful for processing large amounts of data that can't fit into memory at once, like reading large files or processing large database results.
- Answer:
How can you prevent the N+1 query problem in Laravel's Eloquent?
- Answer: You can prevent the N+1 query problem by using eager loading with
with
orload
methods to load related models in a single query.
- Answer: You can prevent the N+1 query problem by using eager loading with
What are Laravel Macros, and how do you define one?
- Answer: Laravel Macros allow you to add custom methods to existing classes (like Collections, Routes, etc.). You can define a macro using the
macro
method. For example:Collection::macro('toUpper', function () { return $this->map(fn($value) => strtoupper($value)); });
- Answer: Laravel Macros allow you to add custom methods to existing classes (like Collections, Routes, etc.). You can define a macro using the
Describe how you can use the
customIf
directive in Blade.- Answer: The
@customIf
directive in Blade allows you to define your custom conditional logic. You register it using theBlade::if
method in a service provider. For example:Blade::if('env', function ($environment) { return app()->environment($environment); });
and use it in Blade:@env('local')
.
- Answer: The
What is the difference between
tap
andwith
helper functions in Laravel?- Answer: The
tap
function allows you to call a given closure with a given value and then return the value, facilitating method chaining. Thewith
function is similar but is used to retrieve a value only after passing it through a closure for further processing.
- Answer: The
Explain the purpose of the
route cache
in Laravel and how you enable it.- Answer: The route cache improves the performance of the application by reducing the time it takes to resolve routes. You can enable it by running the command
php artisan route:cache
.
- Answer: The route cache improves the performance of the application by reducing the time it takes to resolve routes. You can enable it by running the command
How do you implement soft deletes in Laravel, and how do you restore a soft-deleted record?
- Answer: Implement soft deletes by adding the
SoftDeletes
trait to your Eloquent model and including adeleted_at
timestamp column in your table. Restore a soft-deleted record using therestore
method:Model::withTrashed()->find($id)->restore();
.
- Answer: Implement soft deletes by adding the
Would you like more questions or any specific advanced topic covered?