Laravel interview questions and answers 👇
Laravel General Interview Questions
How can you generate URLs?↑
Laravel has helpers to generate URLs. This is helpful when you build link in your templates and API response.
List out common artisan commands used in Laravel↑
- PHP artisan down;
- PHP artisan up;
- PHP artisan make:controller;
- PHP artisan make:model;
- PHP artisan make:migration;
- PHP artisan make:middleware;
In which folder robot.txt is placed?↑
Robot.txt file is placed in Public directory.
Why are migrations necessary?↑
Migrations are necessary because:
- Without migrations, database consistency when sharing an app is almost impossible, especially as more and more people collaborate on the web app.
- Your production database needs to be synced as well.
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, which used to interact with the table.
List available types of relationships in Laravel Eloquent.↑
Types of relationship in Laravel Eloquent are:
- One To One
- One To Many
- Many To Many
- Has Many Through, and
- Polymorphic Relations.
How will you register service providers?↑
You can register service providers in the config/app.php configuration file that contains an array where you can mention the service provider class name.
What is X-XSRF-TOKEN?↑
Laravel stores the current CSRF token in a XSRF-TOKEN cookie that is included with each response generated by the framework. You can use the cookie value to set the X-XSRF-TOKEN request header.
What is benefits of using "Blade" Template in Laravel?↑
Unlike other popular PHP templating engines, Blade does not restrict you from using plain PHP code in your views. In fact, all Blade views are compiled into plain PHP code and cached until they are modified, meaning Blade adds essentially zero overhead to your application. Blade view files use the .blade.php file extension and are typically stored in the resources/views directory.
What is Laravel Elixir?↑
What are common HTTP error codes?↑
- Error 404 – Displays when Page is not found.
- Error- 401 – Displays when an error is not authorized
What is Laravel API rate limit?↑
It is a feature of Laravel. It provides handle throttling. Rate limiting helps Laravel developers to develop a secure application and prevent DOS attacks.
How to get current environment in Laravel?↑
$environment = App::environment();
Explain the concept of cookies.↑
Cookies are small file sent from a particular website and stored on PC by user's browser while the user is browsing.
Lumen is a micro-framework. It is a smaller, and faster, version of a building Laravel based services, and REST API's.
Which file is used to create a connection with the database?↑
To create a connection with the database, you can use .env file.
In which directory controllers are kept in Laravel?↑
Controllers are kept in app/http/Controllers directory.
What does ORM stand for?↑
ORM stands for Object Relational Mapping
What is Laravel?↑
Laravel is an open-source widely used PHP framework. The platform was intended for the development of web application by using MVC architectural pattern. Laravel is released under the MIT license. Therefore, its source code is hosted on GitHub. It is a reliable PHP framework as it follows expressive and accurate language rules.
What are the advantages of using Laravel?↑
- Laravel has blade template engine to create dynamic layouts and increase compiling tasks.
- Reuse code without any hassle.
- Laravel provides you to enforce constraints between multiple DBM objects by using an advanced query builder mechanism.
- The framework has an auto-loading feature, so you don't do manual maintenance and inclusion paths
- The framework helps you to make new tools by using LOC container.
- Laravel offers a version control system that helps with simplified management of migrations.
What is Laravel Forge?↑
Laravel Forge is a tool that helps in organising and designing a web application. Although the manufacturers of the Laravel framework developed it, it can automate the deployment of every web application that works on a PHP server.
It is a method of identifying user login credential with a password. In Laravel it can be managed with a session which takes two parameters:
Where will you define Laravel's Facades?↑
All facades of Laravel are defined in Illuminate\Support\Facades namespace.
Name databases supported by Laravel.↑
- SQL Server
How can you reduce memory usage in Laravel?↑
While processing a large amount of data, you can use the cursor method in order to reduce memory usage.
How to use the custom table in Laravel Model?↑
In order to use a custom table, you can override the property of the protected variable $table.
What are the difference between insert() and insertGetId() in laravel?↑
- insertGetId(): This method is also used for insert records into the database table. This method is used in the case when an id field of the table is auto-incrementing.
It returns the id of current inserted records.
- Inserts(): This method is used for insert records into the database table. No need the "id" should be autoincremented or not in the table.
How will you describe Fillable Attribute in a Laravel model?↑
In eloquent ORM, $fillable attribute is an array containing all those fields of table which can be filled using mass-assignment.
Mass assignment refers to sending an array to the model to directly create a new record in Database.
What is query scope?↑
It is a feature of Laravel where we can reuse similar queries. We do not require to write the same types of queries again in the Laravel project. Once the scope is defined, just call the scope method when querying the model.
What is a raw expression in laravel?↑
Raw Expressions allow you to tell the query builder that you want to use what you entered and not get processed or manipulate before running the query against the database.
State the difference between authentication and authorization.↑
Authentication means confirming user identities through credentials, while authorization refers to gathering access to the system.
Name some Inbuilt Authentication Controllers of Laravel.↑
Explain active record concept in Laravel.↑
In active record, class map to your database table. It helps you to deal with CRUD operation.
In which language Laravel is written ?↑
Laravel is written in PHP.
State the difference between get and post method.↑
Get method allows you to send a limited amount of data in the header. Post allows you to send a large amount of data in the body.
How can we check the logged-in user info in Laravel?↑
User() function is used to get the logged-in user
What is the use of DB facade?↑
DB facade is used to run SQL queries like create, select, update, insert, and delete.
How to check request is ajax or not ?↑
In Laravel, we can use $request->ajax() method to check request is ajax or not.
Name the Template Engine utilized by Laravel.↑
Blade is a powerful template engine utilized by Laravel.
How can we check the Laravel current version?↑
One can easily check the current version of Laravel installation using the -version option of artisan command.
What is laravel spark?↑
Spark is a Laravel package that provides scaffolding for all of the stuff you don't want to code.
Explain Migrations in Laravel.↑
Laravel Migrations are like version control for the database, allowing a team to easily modify and share the application’s database schema. Migrations are typically paired with Laravel’s schema builder to easily build the application’s database schema.
Name aggregates methods of query builder.↑
Aggregates methods of query builder are:
What is Monolog library in Laravel?↑
Laravel utilizes the Monolog library, which provides support for a variety of powerful log handlers.
What is a Controller?↑
A controller is the "C" in the "MVC" (Model-View-Controller) architecture, which is what Laravel is based on.
Explain the concept of contracts in Laravel.↑
They are set of interfaces of Laravel framework. These contracts provide core services. Contracts defined in Laravel include corresponding implementation of framework.
Which class is used to handle exceptions?↑
Laravel exceptions are handled by App\Exceptions\Handler class.
Explain collections in Laravel.↑
Collections is a wrapper class to work with arrays.
How can someone turn off CSRF protection for a specific route?↑
To turn off CSRF protection for a specific route, we can add that specific URL or Route in $except variable which is present in the app\Http\Middleware\VerifyCsrfToken.phpfile.
How will you check table is exists or in the database?↑
Use hasTable() Laravel function to check the desired table is exists in the database or not.
What is the use of dd() function?↑
This function is used to dump contents of a variable to the browser. The full form of dd is Dump and Die.
How can you access session data?↑
Session data be access by creating an instance of the session in HTTP request. Once you get the instance, use get() method with a "Key" as a parameter to get the session details.
Listeners are used to handling events and exceptions. The most common listener in Laravel for login event is LoginListener.
What is make method?↑
Laravel developers can use make method to bind an interface to concreate class. This method returns an instance of the class or interface. Laravel automatically inject dependencies defined in class constructor.
What is Redis?↑
Redis is an open source, advanced key-value store. It is often referred to as a data structure server since keys can contain strings, hashes, lists, sets, and sorted sets.
Define hashing in Laravel.↑
It is the method of converting text into a key that shows the original text. Laravel uses the Hash facade to store the password securely in a hashed manner.
Explain reverse routing in Laravel.↑
Reverse routing is a method of generating URL based on symbol or name. It makes your Laravel application flexible.
Why use Route?↑
Routes are stored inside files under the /routes folder inside the project's root directory. By default, there are a few different files corresponding to the different "sides" of the application ("sides" comes from the hexagonal architecture methodology).
does Laravel support caching?↑
Yes, Laravel supports popular caching backends like Memcached and Redis. By default, Laravel is configured to use the file cache driver, which stores the serialized, cached objects in the file system.For large projects, it is recommended to use Memcached or Redis.
@include is used to load more than one template view files. It helps you to include view within another view. User can also load multiple files in one view.
What is the deployer?↑
Deployer provides a turn-key Kubernetes-based application management platform that accelerates the adoption of open-source and cloud-native technologies.
What do you know about CSRF token in Laravel?↑
CSRF protection stands for Cross-Site Request Forgery protection. CSRF detects unauthorized attacks on web applications by the unauthorized users of a system. The built-in CSRF plug-in is used to create CSRF tokens so that it can verify all the operations and requests sent by an active authenticated user.
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.
Explain important directories used in a common Laravel application.↑
Directories used in a common Laravel application are:
- App/: This is a source folder where our application code lives. All controllers, policies, and models are inside this folder.
- Config/: 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.
How will you explain Guarded Attribute in a Laravel model?↑
The guarded attribute is the opposite of fillable attributes.
In Laravel, fillable attributes are used to specify those fields which are to be mass assigned. Guarded attributes are used to specify those fields which are not mass assignable.
How to hash password in Laravel?↑
The Hash::make function is used to create a hash for the password.
What is a Route?↑
A route is basically an endpoint specified by a URI (Uniform Resource Identifier). It acts as a pointer in Laravel application.
Most commonly, a route simply points to a method on a controller and also dictates which HTTP methods are able to hit that URI.
What is HTTP middleware?↑
HTTP middleware is a technique for filtering HTTP requests. Laravel includes a middleware that checks whether application user is authenticated or not.
How to Install Laravel via Composer?↑
composer create-project --prefer-dist laravel/laravel myproject
How to Install Lumen via Composer?↑
composer create-project --prefer-dist laravel/lumen myproject
How to clear Cache in Laravel?↑
You can use php artisan cache:clear commnad to clear Cache in Laravel.
What is a fluent query builder in Laravel?↑
The Fluent Query Builder is Laravel's powerful fluent interface for building SQL queries and working with your database.
What are policies classes?↑
Policies classes include authorization logic of Laravel application. These classes are used for a particular model or resource.
Explain the concept of events in Laravel.↑
An event is an occurrence or action that help you to subscribe and listen for events that occur in Laravel application. Some of the events are fired automatically by Laravel when any activity occurs.
What is Route Model Binding?↑
When injecting a model ID to a route or controller action, you will often query to retrieve the model that corresponds to that ID. Laravel route model binding provides a convenient way to automatically inject the model instances directly into your routes.
Explain Laravel echo.↑
How to compare two Carbon Timestamps?↑
First, Eloquent automatically converts its timestamps ( created_at, updated_at ) into carbon objects. You could just use updated_at to get that nice feature, or specify edited_at in your model in the $dates property: protected $dates = ['edited_at'];
Explain homestead in the Laravel.↑
Homestead is basically an official, pre-packaged, and vagrant virtual machine that is used to deliver Laravel developers and all the necessary tools in order to develop Laravel out of the box. This machine is also known to include Ubuntu, Gulp, Bower, and various other development tools that are useful in developing full-scale web applications.
Explain Laravel’s Middleware.↑
As the name suggests, Middleware acts as a middleman between request and response. It is a type of filtering mechanism. There are two types of Middleware in Laravel.
- Global Middleware: will run on every HTTP request of the application.
- Route Middleware: will be assigned to a specific route.
What is service container in Laravel?↑
Service container is a tool used for performing dependency injection in Laravel.
How can you make real time sitemap.xml file in Laravel?↑
You can create all web pages of a website to tell the search engine about the organizing site content. The crawlers of search engine read this file intelligently to crawl a website.
What is MVC framework?↑
It is Model, View, and Controller:
- Model: Model defines logic to write Laravel application.
- View: It covers the UI logic of Laravel application.
- Controller: It works as an interface between Model, and View. It is a way how the user interacts with an application.
Explain traits in Laravel.↑
Laravel traits are a group of functions that you include within another class. A trait is like an abstract class. You cannot instantiate directly, but its methods can be used in concreate class.
What are the disadvantages of using Laravel?↑
- Development process requires you to work with standards and should have real understanding of programming
- Laravel is new framework and composer is not so strong in compare to npm (for node.js), ruby gems and python pip.
- Development in laravel is not so fast in compare to ruby on rails.
- Laravel is lightweight so it has less inbuilt support in compare to django and rails. But this problem can be solved by integrating third party tools, but for large and very custom websites it may be a tedious task-
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:
- Constructor injection
- Setter injection
- Interface injection.
What is namespace in Laravel?↑
A namespace allows a user to group the functions, classes, and constants under a specific name.
How to remove a complied class file?↑
Use clear-compiled command to remove the compiled class file.
What do you mean by Laravel Dusk?↑
What is Lazy vs Eager Loading in Laravel?↑
Laravel Eloquent ORM provides two type of loading.
- Lazy Loading: By default, accessing data in eloquent is "Lazy loaded"
- Eager Loading: This can be achieved using with() in Eloquent. Eager loading alleviates the N + 1 query problem.
How to rollback last migration?↑
Use need to use artisan command to rollback the last migration.
Explain PHP artisan.↑
An artisan is a command-line tool of Laravel. It provides commands that help you to build Laravel application without any hassle.
List some default packages provided by Laravel 5.4 ?↑
What is Singleton design pattern?↑
Singleton design pattern is a creational pattern that is used whenever only one instance an object is needed to be created. In this pattern, you can't initialize the class.
What do you mean by bundles?↑
In Laravel, bundles are referred to as packages. These packages are used to increase the functionality of Laravel. A package can have views, configuration, migrations, routes, and tasks.
What is Laravel Envoyer?↑
Envoyer is a zero-down-time deployer for PHP & Laravel projects, which means it is a tool that you connect to your server to run your deploys, and which uses a series of tools to ensure that all of the preparation work each deploy needs in order to run.
What is a session in Laravel?↑
Session is used to pass user information from one web page to another. Laravel provides various drivers like a cookie, array, file, Memcached, and Redis to handle session data.
How to configure a mail-in Laravel?↑
Laravel provides APIs to send an email on local and live server.
Explain faker in Laravel.↑
It is a type of module or packages which are used to create fake data. This data can be used for testing purpose.
It is can also be used to generate:
- Lorem text
How can you enable query log in Laravel?↑
You can use enableQueryLog method to enable query log in Laravel.
What is the use of the bootstrap directory?↑
It is used to initialize a Laravel project. This bootstrap directory contains app.php file that is responsible for bootstrapping the framework.
How to create middleware in Laravel?↑
php artisan make: middleware
What is Closure in Laravel?↑
A Closure is an anonymous function. Closures are often used as callback methods and can be used as a parameter in a function.
What is Ajax in Laravel?↑
Explain Response in Laravel.↑
All controllers and routes should return a response to be sent back to the web browser. Laravel provides various ways to return this response. The most basic response is returning a string from controller or route.
Differentiate between delete() and softDeletes().↑
- delete(): remove all record from the database table.
- softDeletes(): It does not remove the data from the table. It is used to flag any record as deleted.
It is an application-level package manager for PHP. It provides a standard format for managing PHP software dependencies and libraries.
Laravel MCQ Quiz Interview Questions
Which of following Collection method returns all records from Laravel collection?↑
- A) ->all()
- B) ->get()
- C) ->where()
- D) ->whereAll()
View files in Laravel end in↑
- A) .blade.php
- B) .php
- C) .vue
- D) .blade
Which one is the Laravel command line interface?↑
- A) CLI
- B) php artisan
- C) composer
- D) git
Which artisan command is used to remove the compiled class file?↑
- A) clearcompiled
- B) clear compiled
- C) clear-compiled
- D) clear:all
Which directory contain “robot.txt” file ?↑
- A) app
- B) public
- C) config
- D) storage
What is Blade?↑
- A) Template Engine
- B) package
- C) view file
- D) framework
What is @yield used for?↑
- A) to display the contents of a given section.
- B) to extend an layout
- C) to include a file
Which command is used to start laravel server?↑
- A) php artisan serve
- B) php artisan start
- C) php artisan start-server
- D) php artisan project_name
Laravel is based on _____________________.↑
- A) MVVM Design Pattern
- B) MVC Design Pattern
- C) Singleton Design Pattern
- D) Composite Design Pattern
. Which of following methods are used in database migrations classes?↑
- A) execute() and rollback()
- B) up() and down()
- C) run() and delete()
- D) save() and update()
The vendor directory contains↑
- A) Laravel Framework code
- B) Assets
- C) Third-party code
- D) Configuration files
Artisan command to flush the application cache:↑
- A) cache:flush
- B) cache:clear
- C) cache:forget
- D) cache:remove