In this article, we cover logical and conceptual most frequently asked Laravel interview questions.
Laravel is an open-source PHP framework that is known for its simplicity and building highly complex applications with much of an ease. Laravel has a large, expanded, and establishes community which supports collaboration and positive growth.
If you are applying for a Laravel interview, you will be asked such questions which will reflect how much you know, with the least amount of questions.
Most interviewers tend to find a person who has strong concepts and will improve further, in the shortest time. Concerning the applied position, you will be asked questions accordingly.
Suppose if you are applying for an internship, then you will be asked stuff that will show how fast you can and how much you already know. Let’s say if you are applying for a higher position like senior backend developer, then you will be judged more upon your knowledge and experience.
Table of Contents
Q1 : What is the composer? Can we install Laravel project without it?
Composer is a package manager used in web applications. Laravel framework uses the composer to install all the dependencies needed in the project. Even though you can download any project from GitHub but you would still need a composer to install all the Laravel and other packages dependencies.
There is a Laravel installer that allows you to install dependencies, but the composer is the best package manager for Laravel and managing the dependencies.
Q2: What is routing in Laravel?
Routes in Laravel are all the possible entry points that a user can use to access your application. It’s defined on the bases of the directory system of Laravel projects. The routes define were to lead the user after he/she interacts with the interface.
q3: Which architecture pattern does the Laravel framework is categorized as?
Laravel is an MVC framework that stands for ( Model View Controller). This architecture pattern decides the flow of the data and the user interaction.
This pattern is used to separate your application into 3 main layers i.e. models, views, and controllers. Models define the data-related logic correspondence with the database. Views are what the user will see, the interface. Lastly, controllers define all the logic of any transactions.
q4: what is reverse routing?
Route::get('/login', '[email protected]');
{{ HTML::link_to_action('[email protected]') }}
Reverse routing will create a link associated with it and can allow you to pass any parameters. If no parameters are passed, then they will be removed from the link.
q5: how can you list down all the registered routes?
You can use the route: list command to list down all the registered routes in your application.
php artisan route:list

you can add flags to filter out the routes.
php artisan route:list –name=update
php artisan route:list –method=GET
php artisan route:list –method=GET –sort=name
q6: how can you register service providers?
You can add service providers by going to congig/app.php. then scroll down to where there is an array of providers and add to it.

q7: how can you encrypt and decrypt in Laravel?
For some data-sensitive information, we require to encrypt it and then save it in the database. Fields like password, account number, etc. There are many ways to do that.
1) Hash
$user->password = Hash::make($request->get(‘new_password’));
2) encrypt helper
$user = User::findOrFail($id);
$user->fill([
‘secret’ => encrypt($request->secret)
])->save();
3) Crypt façade
$user->fill([
‘secret’ => Crypt::encrypt($request->secret)
])->save();
}
4) Hash façade
$request->user()->fill([
‘password’ => Hash::make($request->newPassword)
])->save();
q8: what are traits?
Traits are several functions that can be embedded in another class. Although, you can’t instantiate it but you can use them in concrete classes. You cant instantiate a trait on its own and it increases the reusability concept.
q9: Explain what are contracts in Laravel?.
They are set of interfaces with several functionalities. These contracts provide core services. Contracts defined in Laravel include corresponding implementation of framework.
q10: What are Facades?
Facades are interface to classes that are static and are available in the service containers. There are many facades in Laravel that provides access to several features of Laravel.
All of Laravel’s facades are defined in the Illuminate\Support\Facades.
q11: What are aggregates methods of query builder?
Query builder is an easier way to create a database and manage all the flow of the data.
Most aggregates function perform calculations on a group of data and return a single value.
Aggregates methods of query builder are:
1) max().
2) min().
3) sum()
4) avg()
5) count().
q12: What are HTTP methods?
HTTP methods are GET, POST, PUT/PATCH, DELETE, UPDATE. These methods interact with routes and fetch data of the user from the URL.
q13: What is HTTP middleware?
HTTP middleware filters out the incoming HTTP requests to make it easier to process. For example, if the current request is not authenticated then return without further interaction the application.
q14: Explain the validation concept in Laravel.
Validations are crucial while developing any Laravel application. Validation checks all the restrains of the fields and requirements before storing it in the database.
$this->validate($request, [
'name' => 'required|max:54',
'email' => 'required',
'postal' => 'required',
'contact' => 'required',
'address' => 'required',
'Picture' => 'image|mimes:jpeg,png,jpg,gif,svg|max:2048'
]);
q15: Difference between get and post method?
Get method sends a limited amount of data and is viewable in the URL while post allows to send a large amount of data while keeping it confidential
q16: What are relationships in Laravel?
Eloquent relationships define relations of the data and how they communicate with each other.
- One to one
- One To Many
- Many To Many
- Has One Through
- Has Many Through
- One To One (Polymorphic)
- One To Many (Polymorphic)
- Many To Many (Polymorphic)
For example, a user has many items in the user cart. All items in the user cart belong to one user.
q17: What is ORM?
Object Relational Mapping can be seen as a layer that maps the defined fields in the eloquent into the database accordingly. This makes creating, adding, and deleting data fields, simpler and easier.
q18: Explain dependency injection and their types.
It is a technique in which one object is dependent on another object. There are three types of
dependency injection:
1) Constructor injection
2) setter injection
3) interface injection
q19: Name the Template Engine utilized by Laravel.
Blade is a powerful template engine utilized by Laravel.
q20: Explain what events in Laravel are.
An event is an occurrence or action correspondence to the user interaction.
Some of the events are fired automatically by Laravel when any activity occurs.
Events like submitting a form, clicking on a button, are fired to listen to the cause of trigger and response accordingly.
q21: What is migration?
Migrations allow to create tables without having to go to the database itself.
You don’t have to go visit phpMyAdmin, SQL, or any other database you are using to manually create a table and fill it with data.
The best part about migrations is that it allows you to drop or add fields in your database without deleting existing records.
q22: What are helpers?
Laravel has various helper functions that are global and can be used anywhere in your application.
They have certain capabilities defined and can fulfill to provide more functionality. You can even create your helper and use them just like the built-in helpers by Laravel.
Here are a few examples.
- Path
- Array
- String
- URLs
q23: What is collection in Laravel?
Collection in Laravel is a powerful tool. It is mostly used by Eloquent but can be separately used.
Collection is an array passed to collect helper function. The results of Eloquent uses collection. You can filter, modify, compare, and do much more with a collection.
q24: Responsibility of Request Class?
To capture the instance of the URL and to pass on the request of the user, the Request class is responsible.
From the request class, a variable $request is usually created to use it further and to compute on the user’s request.
public function store(Request $request)
{
$name = $request->input('name');
}
Here you can use the request variable.
q25: What are Scopes in Models?
Scopes in models shrinks the code to make it more precise. This dries up the code and easy to understand
$Users = User::where('name', =,"codeleaks")->get();
If you want to run this query you can run it as follows.
class User extends Model
{
public function scopeCompleted($query)
{
return $query->where('name', "codeleaks");
}
}
q26: What is Blade Templating Engine?
Blade is a powerful Laravel templating engine. It allows you to write PHP tags in your views and reduces overhead from your application by compiling code in PHP. All the blade templates are defined with a .blade.php extension in the resources/views directory.
q27: Hashing vs Encryption?
Hashing is a way to convert data into a form that is not understandable by humans. In hashing, once a string is hashed, there is no way to find the original string from it.
Whereas in encryption, only the person having a key can decrypt the encrypted string.
Hashing is Laravel is done on password usually and the password given by the user at the time of logging in, that is compared with the original pass.
q28: What is Namespace?
Namespace is a way to divide your code into logical parts so it’s easier to debug, understand, and modify.
When the code written under the namespace is defined then it will be identified by it.
namespace App\Http\Controllers;
you can extend the namespace above with multiple files.
q29: Authentication vs Authorization?
Authentication and authorization do interchange but they still are different. Authentication is to check if the person is what he claims to be.
To check his identity which authentication is giving access to the authenticated person. Even though mostly this happens parallel in Laravel, that’s why most fail to note the difference.
q30: What are the advantages of using Laravel?
- Laravel has a blade template engine to create dynamic layouts and decrease overhead.
- Reuse code easily
- Laravel provides you to enforce constraints between multiple DBM objects by using an advanced query builder mechanism.
- Laravel offers a version control system that helps with simplified management of migrations.
q31: What are bundles?
Bundles are also known as packages in Laravel. These packages increase the functionality of the application. A package can have views, configuration, migrations, routes, and tasks. And can be easily added to your program.
q32: What is Lumen
Lumen is a micro-framework. It is a smaller and faster, version of a building Laravel based services, and REST API’s.
q33: What are Accessors and Mutators?
Mutators and accessors of Eloquent ORM are a handful of ways to modify data from the database.
More precisely, a mutator allows you to modify data before being saved into the database. While, an accessor allows you to modify data after it’s been fetched from the database.
q34: Sessions vs Cookies? What's the difference?
Cookies store data on the client-side to increase functionality. While sessions store data server-side across the request.
The session configuration file is stored in config/session.php. Cookies store small amounts of data on the client-side so that the data needed while the user interaction would be easily fetched.
For example, for a shopping cart, the information of the items in the cart must be stored and easily accessed. Cookies store that info instead of sending it to the server-side.
q35: How can you use multiple were clauses in the query builder?
There are 2 ways in which you can incorporate this.
$results = User::where('this', '=', 1)
->where('that', '=', 1)
->where('this_too', '=', 1)
->where('that_too', '=', 1)
->where('this_as_well', '=', 1)
->where('that_as_well', '=', 1)
->where('this_one_too', '=', 1)
->where('that_one_too', '=', 1)
->where('this_one_as_well', '=', 1)
->where('that_one_as_well', '=', 1)
->get();
Use multiple where clauses but its not elegant.
Another way is to use a much compact alternative.
$query->where([
['column_1', '=', 'value_1'],
['column_2', '<>', 'value_2'],
[COLUMN, OPERATOR, VALUE],
...
])
This is a much neater and simpler way.
q36: How can you check if there exists a record?
You can get the first record with the matching condition with where and check if its null.
user = User::where('name', '=', Input::get('name'))->first();
if ($user === null) {
// user doesn't exist
}
Here is another way to do this
if (User::where('name', '=', Input::get('name'))->count() > 0) {
// user found
}
And a more direct way
if (User::where('name', '=', Input::get('name'))->exists()) {
// user found
}
q37: how can you reset the user's password?
If you have a user controller then add a function to change the password.
public function changePassword(Request $request, $id)
{
$this->validate($request,[
'old_password'=>'required|min:6',
'new_password'=>'required|min:6',
'confirm_password'=>'required|min:6|required_with:new_password|same:new_password',
]);
//if old pass is not matched
if (!(Hash::check($request->get('old_password'), Auth::user()->password))) {
return back()->with('error', "Incorrect Password Entered.");
}
$user = Auth::user();
$user->password = Hash::make($request->get('new_password'));
$user->save();
return back()->with('success', "Password Changed! ");
}
Now there is validation as well before saving anything in the database.
q38: Name databases supported by Laravel.
• PostgreSQL
• SQL Server
• SQLite
• MySQL
q39: how can you reduce memory usage?
You can reduce memory usage through the cursor method. Cursor method allows us to traverse over the large amount of data through a pointer instead of iterating over each record.
This help in reducing the memory usage though access only necessary data
q40: what is PHP artisan?
Artisan is a command-line tool of Laravel. It provides commands that help you to build your application easily by just executing a few commands.
q41: What is dd() ?
Dump and die function is a great tool to find errors, understand data flow etc. dump and die “dumps” everything passed in its parameter and terminates the application. When you don’t want all the code to execute, dd() is the right tool for it.
q42: Difference between delete() and softDelete()?
delete() remove all the records from the database while softDelete() flags the data as deleted while still keeping the record.
q43: What is Auth in Laravel?
Auth provides authentication by Laravel and you don’t have to set constraints on your own. You can run the command php artisan make:auth to have all the necessities
namespace App\Http\Controllers;
use App\Http\Requests;
use Illuminate\Http\Request;
class HomeController extends Controller{
/**
* Create a new controller instance.
*
* @return void
*/
public function __construct() {
$this->middleware('auth');
}
q44: How to clear Cache in Laravel?
You can use php artisan cache:clear command to clear cache in Laravel.
q45: What is name binding in routes?
When defining routes you can bind them with name and later call them with that name instead of using the whole controller and index name.
Route::pos(/create_agent''[email protected]')->name('create_agent');
q46: What class is used to handle exceptions?
Class App\Exceptions\Handler handles the exceptions in Laravel.
q47: Difference between Insert() and insertGetId()?
Insert() only insert data without any condition in the database. Whereas, insertGetId() first checks the id then insert it in the matching record.
q48: Define Laravel guard.
Laravel guard is a special component that is used to find authenticated users. The incoming requested is initially routed through this guard to validate credentials entered by users. Guards are defined in ../config/auth.php file.
q49: Define Implicit Controller.
Implicit Controllers help you to define a proper route to handle controller action. You can define them in route.php file with Route:: controller() method.