Laravel Interview Questions and Answers
1. What is MVC architecture?
It stands for Model View Controller. It segregates domain, applications, business, and logic from the user interface. It is achieved by separating the application into three parts:
Model: Data and data management of the application
View: The user interface of the application
Controller: It handles actions and updates
2. Define composer
Composer is an application-level package manager for PHP. It provides a standard format for managing PHP software dependencies and libraries.
3. What is a Route?
A route is an endpoint specified by a URI (Uniform Resource Identifier). It acts as a pointer in the Laravel application. It simply points to a method on a controller and also dictates which HTTP methods are able to hit that URI.
4. Tell me some important directories used in a common Laravel application.
App/:
It is a source folder where our application code lives. All controllers, policies, and models are inside this folder.
Config/:
It holds the app’s configuration files. These are usually not modified directly but instead, rely on the values set up in the .env (environment) file at the root of the app.
Database/:
Houses the database files, including migrations, seeds, and test factories.
Public/:
Publicly accessible folder holding compiled assets and of course an index.php file.
5. What is a Controller?
Controllers are meant to group associated request-handling logic within a single class. In the Laravel project, they are stored in the app/Http/Controllers directory. It acts as a directing traffic between Views and Models.
6. Which template engine is used by Laravel?
The blade is a simple, powerful templating engine provided by Laravel. There is no restriction to use PHP codes in the views. All the blade views are compiled into simple PHP code and cached until they are modified. Blade view files in Laravel use the .blade.php
file extension and are saved in the resources/views
directory.
7. What is an artisan?
An artisan is a command-line tool of Laravel. It provides commands that help you to build a Laravel application without any hassle.
8. What is Auth?
Auth is an in-built functionality provided by Laravel to identify the user credentials with the database. It takes input parameters like username and password for user identification. If the settings match, then the user is said to be authenticated.
9. What are Models in Laravel?
A model is used as a way for questioning data to and from the table within the database. Models interact with your database and recover your objects’ information. Finally, views render pages.
10. How to Create a Laravel Project?
Method-1:
composer create-project laravel/laravel <project-name>
Method-2:
composer global require laravel/installer
laravel new <project-name>
11. What do you understand by ORM?
ORM stands for Object-Relational Mapping. It is one of the main features of the Laravel framework. It may be defined as an advanced PHP implementation of the active record pattern.
12. Which file is used to create a connection with the database?
To create a connection with the database, you can use .env
file.
13. What is Eloquent?
Eloquent is an ORM used in Laravel. It provides simple active record implementation working with the database. Each database table has its Model that is used to interact with the table.
14. What are Events in Laravel?
Events are great for decoupling different parts of your application. It is typically stored in the app/Events directory.
15. How to run a Laravel project on localhost?
php artisan serve
16. Define Lumen
php artisan serve
16. Define Lumen
Lumen is a smaller and faster micro-framework that is used to build Laravel-based services and REST APIs.
17. How can we use the custom table in Laravel?
We can easily use the custom table in Laravel by overriding the protected $table property of Eloquent.
Example:
class User extends Eloquent{ protected $table="my_user_table"; }
18. Define @include
@include
is used to load more than one template view file. It helps you to include a view within another view.
19. What is a session in Laravel?
In Laravel, a session is used to store data and keeps track of users.
20. Difference between authentication and authorization.
Authentication means confirming user identities through credentials.
Authorization refers to gathering access to the system.
21. What are seeders in Laravel?
It allows you to quickly insert data into your database. Seeders are very helpful for development environments where you may not have access to your production database.
22. Define factories in Laravel.
In Laravel, the factory feature allows you to build fake data for your models. It is very useful for testing and seeding counterfeit data into your database to see your code in action before any accurate user data comes in.
23. What is Middleware in Laravel?
In Laravel, Middleware is a platform that works as a bridge between the request and the response. The main aim of middleware is to provide a mechanism for investigating HTTP requests entering your application.
24. How to make a new Model in Laravel?
The make:model
command is used to generate new model in laravel.
php artisan make:model ModelName
25. What is Dependency injection in Laravel?
Dependency injection is a term used for the activity of injecting components into the user application. It’s a critical element of agile architecture.
26. Define REPL.
It stands for Read—Eval—Print—Loop. REPL is a type of interactive shell that takes in single user inputs, processes them, and returns the result to the client.
27. What is a yield in Laravel?
In Laravel, It is a function used to define a particular section in layout. It is permanently used to retrieve content from the child page and transfer it to the master page.
28. How to create a controller in Laravel?
To create a new controller, you may run the make:controller
Artisan command. By default, all of the controllers for your application are stored in the app/Http/Controllers
directory:
php artisan make:controller ControllerName
29. Define Migrations in Laravel.
In Laravel, migration is a feature of Laravel that allows you to modify and share the application’s database schema. It’ll enables you to alter the table by adding a new column or deleting an existing column.
30. What is Closure in Laravel?
The closure is an anonymous function. It is often used as a callback method and as a parameter in a function.