Laravel interview questions

Laravel interview questions | Freshers & Experienced

  • Sharad Jaiswal
  • 17th Nov, 2019
  • 802 Followers

Laravel interview questions for 3, 4, 5 years experience

Q1. What are pros and cons of using Laravel Framework?

Pros of Laravel

  1. It uses Blade template engine that helps in speed up compilation tasks and writing cleaner code.
  2. Bundled modularity forces code reusability.
  3. Easy to understand Eloquent ORM.
  4. Outstanding Command Line Interface Artisan.
  5. Clean, clear easily understandable documentation.

Cons of Laravel

  1. Quite slow because of loading multiple vendors.
  2. Amature developers face some difficulties in staring, but once they are familiar it will be fun to code in Laravel.
  3. Community support is not as wide as a comparison to other frameworks.
  4. Upgrading framework is a headache.

Q2. What is Laravel service container?

Service Container in Laravel is a Dependency Injection Container and a Registry for the application. It is one of the most powerful tool for managing class dependencies and performing dependency injection.

Q3. How To Use Delete Query In Laravel ?

DB::table('table_name')->delete($id);

is the simplest way to delete a record in Laravel.

Q4. How To Use Update Query In Laravel ?

Updating a record in Laravel using Eloquent.

$user=User::where(['id'=>1])->first();
$user->name='abc';
$user->age='22';
$user->save();

Q5. What is Laravel Dusk?

Laravel Dusk is browser automation and testing tool introduced in Laravel 5.4. It uses ChromeDriver to perform browser automation testing.

Q6. What is Laravel Echo?

Laravel Echo is a tool that makes it easy for you to bring the power of WebSockets to your Laravel applications. It simplifies some of the more common—and more complex—aspects of building complex WebSockets interactions.

Echo comes in two parts: a series of improvements to Laravel's Event broadcasting system, and a new JavaScript package.

Q7. What is Laravel Homestead ?

Laravel Homestead is an official, pre-packaged Vagrant "box" that provides you a wonderful development environment without requiring you to install PHP, HHVM, a web server, and any other server software on your local machine.

Q8. What is Tagging?

Tagging is a package in Laravel that allow you to tag your content like pages/ post to a keyword.

Q9. What is dependency injection in Laravel ?

Dependency injection or (D.I) is a technique in software Engineering whereby one object (or static method) supplies the dependencies of another object. A dependency is an object that can be used (a service). Injection is the passing of dependency to a dependent object (a client) that would use it.

Basically, You can found 3 types of dependency injection:

  • Constructor injection
  • Setter injection
  • Interface injection

Q10. What are named routes in Laravel?

Named routing is another amazing feature of the Laravel framework. Named routes allow referring to routes when generating redirects or URLs more comfortably. You can specify named routes by chaining the name method onto the route definition:

Example:

Route::get('user/profile', function () {
    //
})->name('profile');

You can specify route names for controller actions:

Route::get('user/profile', 'UserController@showProfile')->name('profile');

Once you have assigned a name to your routes, you may use the route's name when generating URLs or redirects via the global route function:

// Generating URLs...
$url = route('profile');

// Generating Redirects...
return redirect()->route('profile');

Q11. What are Laravel eloquent?

Eloquent is an ORM in Laravel that implements active record pattern and is used to interact with relational databases.

Q12. What is Lumen?

Lumen is micro-framework by Laravel. It is developed by the creator of Laravel Taylor Otwell for creating smart and blazing fast API’s. Lumen is built on top components of Laravel. As Lumen is a micro-framework not a complete web framework like Laravel and used for creating API’s only, so most of the components as HTTP sessions, cookies, and templating are excluded from Lumen and only features like routing, logging, caching, queues, validation, error handling, database abstraction, controllers, middleware, dependency injection, Blade templating, command scheduler, the service container, and the Eloquent ORM are kept.

Q13. What is a Facade?

In Programming, Facade is a software design pattern which is often used in object-oriented programming. Laravel facade is a class which provides a static-like interface to services inside the container.

Q14. How To Use Delete Statement In Laravel?

Use delete statement in Laravel:

In Query Builder Way:

\DB::table('users')->delete($id); // delete with id
\DB::table('users')->where('name', $name)->delete(); \\ delete with where condition

In Eloquent Way:

 User::find($id)->delete() // delete with id
 User::where(['name'=>$name])->delete();  \\ delete with where condition

Q15. Difference between Contracts and Facades?

Contracts are an interface, while Facades are not an interface. it is a class. 

Contracts are a set of interfaces that define the core services provided by the framework, while Facades provide a static interface to classes that are available in the application's service container.

 

Q16. What is an Observer in Laravel?

Q17. How to get current Url in Laravel ?

In Laravel 5.5 or above you can use url()->current(); to get current URL without query string and  url()->full(); with the query string.

Q18. How To Use Select Query In Laravel?

DB::select('name')->table('users')->get();

This will create a collection that only contains the 'name' property of ever user...

DB::select('name','username')->table('users')->get();

This will select name and username from users

Q19. Which template engine Laravel use ?

Laravel uses Blade template Engine.

Q20. How to use custom table in Laravel Model ?

To use custom table name in Laravel Model add below code to your Model file.

protected $table = 'custom_table_name';

Q21. What are Closures in laravel ?

A Closure is an anonymous function that often used as callback methods and can be used as a parameter in a function.

Example of Laravel Closure

User::with('profile', function ($builder) {
	// Get me all collapsed comments
	return $builder->whereCollapsed(true);
	
});

Q22. How to get Logged in user info in Laravel ?

You can use Auth Facade to get logged in user information in Framework.

Example

<?php 
$userInfo= Auth::user();
echo $userInfo->id;
echo $userInfo->name;
dd( $userInfo);
?>

Q23. Does laravel support php 7?

Yes, Laravel supports PHP 7.

Q24. What is Laravel Elixir ?

Laravel Elixir provides a clean, fluent API for defining basic Gulp tasks for your Laravel application. Elixir supports several common CSS and JavaScript pre-processors, and even testing tools. Using method chaining, Elixir allows you to fluently define your asset pipeline.

Laravel Elixir Example

elixir(function(mix) {
    mix.sass('app.scss')
       .coffee('app.coffee');
});

Q25. How can you display HTML with Blade in laravel ?

Blade template engine provides an easy and clean way to display HTML in the Laravel view. Use {!! $your_variable !!} in your view.

Q26. How to install Laravel via composer? Syntax

Use the below command to install or new project in Laravel.

composer create-project Laravel/Laravel your-project-name version

Note: The composer must be installed globally to run the above command.

Q27. What are Cookies ? How to get , set , destroy cookies in Laravel ?

Cookies are a small amount of data sent from specific websites to the clients computers. Cookies are stored on user computer using browsers. Cookies are important for working with the user's session on a web application.

In Laravel, you can create cookie using Cookie helper. Cookie helper is a global helper and an instance of Symfony\Component\HttpFoundation\Cookie class.

Setting Cookie in Laravel.

<?php 
	$cookie_name="user_name";
	$cookie_value="PhpScots";
	$cookie_expired_in=3600;//in mins
	$cookie_path='/'; // available to all pages of website.
	$cookie_host=$request->getHttpHost(); // domain or website you are $http_only=false;
	$my_cookie= cookie($cookie_name, $cookie_value,$cookie_expired_in,$cookie_path,$cookie_host,$http_only);
	return response()->withCookie($my_cookie);
?>

Getting Cookie in Laravel.

<?php
	return Cookie::get('cookie_name');
?>

Destroying Cookie in Laravel.

<?php
$cookie = Cookie::forget('cookie_name');
?>

Q28. How can you Exclude URIs From CSRF Protection in Laravel ?

Q29. What getFacadeAccessor method does?

Laravel Facades are another way to use classes without manually creating an object of the class. Examples of Laravel Facades are DB, Cache, Cookie, etc.

getFacadeAccessor() method in Laravel is used to return the name of a service container binding.

Example:

class ABC extends Facade
{
    /**
     * Get the registered name of the component.
     *
     * @return string
     */
    protected static function getFacadeAccessor() { return 'abc'; }
}

In the above example whenever the user calls any static method on ABC class then Laravel automatically resolves the abc class binding from the service container and runs the requested method/function against that object.

About Author :

  • Author of Laravel interview questions

    Sharad Jaiswal

    My name is Sharad Jaiswal, and I am the founder of Conax web Solutions. My tech stacks are PHP, NodeJS, Angular, React. I love to write technical articles and programming blogs.

Leave A Comment :

Valid name is required.

Valid name is required.

Valid email id is required.

Explore Popular Courses on Laravel

Start working with Landrick that can provide everything you need to generate awareness, drive traffic, connect.