Laravel Framework
The most popular enterprise framework in the Pakistani IT sector. Focuses on Eloquent internals, the Service Container, N+1 issues, and queue management.
Interview Questions
25 TotalWhat is the N+1 query problem and how do you fix it in Eloquent?
N+1 occurs when you fetch N records and then execute an additional query for each record to fetch relationships. In Laravel, it is solved using Eager Loading via the `with()` method.
How does the Laravel Service Container resolve dependencies?
It resolves dependencies automatically using PHP Reflection. If a class relies on an interface, the container checks its bindings to provide the correct defined implementation.
What are Laravel Accessors and Mutators?
Accessors and mutators allow you to format Eloquent attribute values when you retrieve or set them on model instances (e.g. capitalizing a name or hashing a password).
What is a Middleware in Laravel?
Middleware provides a convenient mechanism for inspecting and filtering HTTP requests entering the application, commonly used for authentication or logging.
How do you handle heavy background tasks in Laravel?
By using Laravel Queues. We can push time-consuming tasks like sending emails or processing images to a queue (like Redis or database) to be handled asynchronously by workers.
What is the purpose of the 'Route::resource' method in Laravel?
The 'Route::resource' method is used to create RESTful routes for a given resource, such as a model, and handles CRUD (Create, Read, Update, Delete) operations.
How do you implement authentication in a Laravel application?
Laravel provides a built-in authentication system using the 'auth' middleware, and you can also use packages like Laravel Socialite for social media authentication or Laravel Passport for API authentication.
What is the difference between 'eloquent' and 'query builder' in Laravel?
Eloquent is an ORM (Object-Relational Mapping) system that provides a simple and expressive way to interact with the database, while the Query Builder is a lower-level system that allows you to construct and execute database queries.
How do you validate user input in a Laravel application?
Laravel provides a validation system through the 'Validator' facade, and you can also use form requests to validate user input.
What is the purpose of the 'AppServiceProvider' in a Laravel application?
The 'AppServiceProvider' is used to register and boot application services, such as event listeners, middleware, and view composers.
How do you use middleware in a Laravel application?
Middleware can be used to filter incoming requests and modify the response, and you can apply middleware to routes, route groups, or controllers.
What is the difference between 'has-one' and 'has-many' relationships in Eloquent?
A 'has-one' relationship is used when a model has one related instance, while a 'has-many' relationship is used when a model has multiple related instances.
How do you implement caching in a Laravel application?
Laravel provides a caching system through the 'Cache' facade, and you can use drivers like file, Memcached, or Redis to store cache data.
What is the purpose of the '.env' file in a Laravel application?
The '.env' file is used to store environment-specific configuration variables, such as database credentials, API keys, and other sensitive information.
How do you use events in a Laravel application?
Events can be used to trigger actions when a specific event occurs, such as when a user is created or updated, and you can use event listeners to handle these events.
What is the difference between 'sync' and 'attach' methods in Eloquent?
The 'sync' method is used to update the related models, while the 'attach' method is used to add new related models.
How do you implement rate limiting in a Laravel application?
Laravel provides a rate limiting system through the 'throttle' middleware, and you can use this middleware to limit the number of requests from a given IP address.
What is the purpose of the 'Controller' class in a Laravel application?
The 'Controller' class is used to handle incoming requests and return responses, and you can use controllers to organize your application logic.
How do you use polymorphic relationships in Eloquent?
Polymorphic relationships allow you to define a relationship between a model and multiple other models, and you can use the 'morphTo' and 'morphMany' methods to define these relationships.
What is the difference between 'Soft Deletes' and 'Force Deletes' in Eloquent?
Soft Deletes mark a model as deleted without actually removing it from the database, while Force Deletes permanently remove the model from the database.
How do you use database transactions in a Laravel application?
Database transactions can be used to group multiple database operations into a single, atomic operation, and you can use the 'DB::transaction' method to define a transaction.
What is the purpose of the 'Request' class in a Laravel application?
The 'Request' class is used to access and manipulate the incoming request data, and you can use this class to retrieve input data, headers, and other request information.
How do you implement API authentication in a Laravel application?
Laravel provides API authentication through the 'passport' package, and you can use this package to generate API tokens and authenticate API requests.
What is the difference between 'Eager Loading' and 'Lazy Loading' in Eloquent?
Eager Loading loads related models in a single query, while Lazy Loading loads related models on demand.
How do you use queue jobs in a Laravel application?
Queue jobs can be used to run tasks in the background, and you can use the 'dispatch' method to dispatch a job to the queue.