Information Technology

Laravel Interview Questions and Answers

Laravel Interview Questions and Answers

Laravel Interview Questions and Answers

1. What is Laravel? What are the specialities of Laravel?
Laravel is a popular open-source PHP framework used for web application development. It was created by Taylor Otwell in 2011 and has since gained widespread adoption due to its elegant syntax, developer-friendly features, and robust ecosystem.

Here are some of its key specialties:
- Elegant Syntax
- Artisan CLI
- ORM (Object-Relational Mapping)
- Custom Routing
- Supports Blade Template Engine
- Built-in Authentication Feature
- Built-in Unit Testing Feature
- Task Scheduling and Queues
- Wide Community Support
- Rich Ecosystem

2. What is Eloquent ORM?
Eloquent ORM is a programming technique which facilitates the mapping of relational database to an object-oriented environment. In eloquent ORM, there is unique model class for each database table. So instead of directly writing sql queries, eloquent ORM provides us convenient object-oriented syntax to perform database operations.
 
It also offers powerful features like relationships, query scopes and eager loading, which further streamline database interactions and enhance code readability and maintainability.
 
3. What is Eager loading?
Eager loading is a programming technique used to efficiently load related model data along with the primary model data in a single database query. It solves the problem of the N+1 query issue and also significantly optimizes performance by reducing the number of queries executed against the database.
 
Ex:
$posts = Post::with('comments')->get();
 
4. What is Lazy loading?
Lazy loading is a programming technique which defers the loading of related model data until it's explicitly requested. This means that when you retrieve a primary model, its related models are not loaded immediately.
 
Ex:
// Retrieve a post
$post = Post::find(1);
 
// Access the comments relationship (comments will be loaded from the database at this point)
$comments = $post->comments;
 
5. What is Dependency injection?
Dependency injection is a programming technique through which classes use external dependencies, rather than creating it itself. We can inject service or class dependencies through the constructor or setter methods. No need to create Service or class instance with 'new' keyword manually.
 
Ex:
class UserManager {
 
 public function __construct(
    protected Logger $log
 ) { }
 
 public function registerUser($name)
 {
   // user registration code
 
   // log the user registration
   $this->log->log($name);
 }
}
 
6. What is Service container?
Service container is a powerful tool for managing class dependencies and performing dependency injection. It keeps track of all service providers objects and dependencies objects.
 
7. What is Service provider?
Service provider is a mechanism through which we can bind or register services into service container. Service provider is a bridge between services and service container.
 
8. What is Collection?
In Laravel, a collection is a powerful utility class provided by the framework that extends PHP's built-in array functions with additional methods to manipulate and work with sets of data more efficiently.
 
Ex:

use Illuminate\Support\Collection;

// Create a collection from an array
$col = collect([1, 2, 3, 4, 5]);

// Filter to get only even numbers
$evenNumbers = $col->filter(
  function ($no)
  {
      return $no% 2 === 0;
  }
);

9. What is Repository Pattern?
The Repository Pattern is a design pattern that separates data access logic from business/presentation logic. It provides a way to centralize data access logic and provides a simple and clean interface to access data from different sources like a database, cache or external API.
 
10. What is Middleware? Give one example.
Middleware is a mechanism to perform HTTP request inspection and filtering according to the application requirement.
 
Ex. Middleware for counting for blog's visits, Middleware for authentication etc.
 
11. How to check whether you have installed the composer on your computer?
We can check whether composer is installed or not using follwing command:
$ composer
 
12. How can we check version of laravel framework?
We can check laravel version with the following command:
$ php artisan --version
 
13. Which command we can use to get a list of available commands?
We can use following command to get a list of available commands:
$ php artisan list
 
14. What are bundles in laravel?
Bundles are used to extends the functionality of Laravel. In Laravel, bundles are popularly known as packages. It contains configuration, routes, migrations, views, etc.
 
15. What is Rounting in laravel?
In laravel, routing is a concept used to bind requests or URLs to their appropriate controller actions/methods.
 
16. What is difference between factories and seeders?
In Laravel, both factories and seeders are used for populating databases with dummy data. Factories are more focused on generating fake data for testing purposes, while seeders are more focused on seeding initial or default data into the database.
 
17. Which command we can use to get list of all registered routes?
We can use following command to get list of all registered routes:
$ php artisan route:list
 
18. What is difference between authentication and authorization?
- Authentication is the process of verifying application users, by using username-password, biometric scans, smart cards etc.
 
- Authorization is the process of verifying required permissions to access the requested resources.
 
19. What is a CSRF token?
In laravel, there is need to add '@csrf' to each form. Laravel will automatically generate a hidden CSRF token field within the forms. When the client submits a form or makes a request, the CSRF token is also submitted along with it. Upon receiving the request, the server verifies that the CSRF token matches the expected value for that user's session. If the token is missing or incorrect, the server rejects the request, thus preventing CSRF attacks. By requiring the presence of a CSRF token in every request that modifies state (e.g., changing user settings, making a transaction), web applications can ensure that the request originated from the same site and not from a malicious source attempting to exploit the user's session.
 
20. What is Facade?
A Facade is a proxy to a dependencies registered in the service container. Facade provides a static interface to underlying classes in the service container, allowing you to access their methods in a more readable and expressive way.
 
21. What is Queue in Laravel?
In Laravel, a queue is a mechanism for deferring the processing of time-consuming tasks or jobs, allowing them to be executed asynchronously in the background. Queues are particularly useful for tasks that are not immediately required for the response to a user's request, such as sending emails, processing images, or performing complex calculations.
 
22. What is Event broadcasting?
Laravel's event broadcasting allows you to broadcast your server-side Laravel events to your client-side JavaScript application using a driver-based approach to WebSockets. Currently, Laravel ships with Pusher Channels and Ably drivers.
 
23. Define Composer.
Composer is the package/dependency manager for the framework. It helps in adding new packages from the huge community into your laravel application.
 
24. What are named routes?
In Laravel, named routes are routes that are assigned a unique name when defined in the application's route files. These names serve as aliases for the routes, allowing you to reference them by name instead of using their full URL paths when generating URLs or redirects within your application.
 
25. What is reverse routing?
In laravel, reverse routing is the process of generating URLs on the basis of named routes. It converts route-name to its associative URL.
 
26. How can we create new laravel projects?
There are two ways to create laravel projects:
 
- First way:  
$ composer create-project laravel/laravel testProject
This command helps us to install laravel-installer everytime and then it creates laravel project.
Means everytime it creates laravel project with the latest laravel version.
 
- Second way:
$ composer global require laravel/installer
$ laravel new testProject
You need to execute first commnad only once. It installs laravel installer.
Second command creates laravel project as per installed laravel installer.
 
27. What is migration?
Migration is the mechanism or process through which developers can add, modify, delete tables. Due to the migration, track of database changes will be maintained properly.
 
28. What are Mutators and Accessors?
- Mutators: Mutators are methods defined within an Eloquent model that allow you to automatically manipulate attribute values before they are saved to the database.
 
- Accessors: Accessors are methods defined within an Eloquent model that allow you to automatically manipulate attribute values when retrieving them from the database.
 
29. Difference between CSRF and XSS attacks?
- CSRF Attack: CSRF attack is a kind of cyber attack in which a malicious website or link generates requests to another website, on behalf of the user, where the user is authenticated. This can lead to unauthorized actions being performed on the target website, such as transferring funds, changing settings or posting content.
 
- XSS Attack: An XSS (Cross-Site Scripting) attack is a type of cyber attack where attackers inject malicious scripts (javascript code) into web browser, which enables attackers to steal sensitive authenticated session data or cookie data. Attackers misuse such data to perform unauthorized actions.
 
30. What are Policies and Gates?
- Policies: In Laravel, policies are classes that define authorization logic for a particular model or resource. They are used to determine if a user is authorized to perform certain actions on the model or resource.
 
- Gates: Gates allow you to define an authorization rule using a simple closure-based approach.