Wednesday, May 3, 2017

Top Laravel Interview Questions with Answers

Laravel is a open-source PHP framework developed by Taylor Otwell used for Developing the websites. Laravel helps you create applications using simple, expressive syntax. Now a days Laravel is very popular PHP framework so mostly companies works on this framework. Here we are listing top Laravel interview questions with answers.



1. What is Laravel?
Laravel is a open-source PHP framework developed by Taylor Otwell used for Developing the websites. Laravel helps you create applications using simple, expressive syntax.

2. What are Advantages of Laravel?

  • Easy and consistent syntax
  • Set-up process is easy
  • customization process is easy
  • code is always regimented with Laravel
3. Explain about Laravel Project?
Laravel is one of the most popular PHP frameworks used for Web Development.
This framework is with expressive, elegant syntax.
It is based on model–view–controller (MVC) architectural pattern.

4. What are the feature of Laravel 5.4?
  • Introducing Laravel Dusk
  • Introducing Laravel Mix 
  • New Markdown Mail in Laravel 5.4 
  • Slots and Components in Laravel 5.4
  • Real-time (automatic) Facades in Laravel 5.4
  • TrimStrings and ConvertEmptyStringsToNull middleware in Laravel 5.4
  • JSON localization files in Laravel 5.4
  • Binding method calls in the service container in Laravel 5.4 
  • The --model flag for creating better resourceful controllers in Laravel 5.4
  • Laravel Collections' updates--higher order messaging and new methods--in Laravel 5.4 
5. Compare Laravel with Codeigniter?
        Laravel                                                                                        Codeigniter
Laravel is a framework with expressive, CodeIgniter is a powerful PHP                       elegant syntax                                                                    framework
Development is enjoyable, creative                                 Simple and elegant toolkit to create full-         experience                                                                         featured web applications.
Laravel is built for latest version of PHP                         Codeigniter is an older more mature                                                                                                         framework
It is more object oriented compared                                 It is less object oriented compared to               to CodeIgniter.                                                                  Laravel.
Laravel community is still small, but it is                        Codeigniter community is large.
growing very fast.

6. What are Bundles,Reverse Routing and The IoC container ?

Bundles: These are small functionality which you may download to add to your web application.
Reverse Routing: This allows you to change your routes and application will update all of the relevant links as per this link.
IoC container: It gives you Control gives you a method for generating new objects and optionally instantiating and referencing singletons.

Also Read: PHP Interview Questions with Answers

7. How to set Database connection in Laravel?
Database configuration file path is : config/database.php
Following are sample of database file

[code]  'mysql' => [
    'read' => [
        'host' => 'localhost',
    ],
    'write' => [
        'host' => 'localhost'
    ],
    'driver'    => 'mysql',
    'database'  => 'database',
    'username'  => 'root',
    'password'  => '',
    'charset'   => 'utf8',
    'collation' => 'utf8_unicode_ci',
    'prefix'    => '',
], [/code]

8. How to enable the Query Logging?
[code] DB::connection()->enableQueryLog();[/code]

9. How to use select query in Laravel?
[code] $users = DB::select('select * from users where city_id = ?', 10);
if(!empty($users)){
    foreach($users as $user){

    }
} [/code]

9. How to use Insert Statement in Laravel?
[code] DB::insert('insert into users (id, name, city_id) values (?, ?)', [1, 'Web technology',10]); [/code]

10. How to use Update Statement in Laravel?
[code]  DB::update('update users set city_id = 10 where id = ?', [1015]); [/code]

11. How to use Update Statement in Laravel?
[code]  DB::update('update users set city_id = 10 where id = ?', [1015]);[/code]

12. How to use delete Statement in Laravel?
[code] DB::delete('delete from  users where id = ?', [1015]); [/code]

13. Does Laravel support caching?
Yes, Its provides.

Also Read: Top Zend Framework Interview Questions with Answers

14. I've already created a database table that I want to use with Laravel's ORM. How would I setup a class to do that?
Laravel's ORM is called Eloquent. There are two main ways to go about doing this. The first one would be to physically write a class:
[code] <?php
namespace App;
use Illuminate\Database\Eloquent\Model;

class Student extends Model
{
    /**
     * The database table used by the model.
     *
     * @var string
     */
    protected $table = 'students';
}[/code]

And the second one would be to use the artisan CLI, which generates a class:
[code] php artisan make:model students [/code]

15. Laravel comes with a PHP CLI called artisan. What is your favorite artisan command?
There are so many things that the CLI does out of the box. Even if they don't have a favorite command, they should be able to explain what some of them do. Here's a sample of available top-level commands:
Available commands:
  •   clear-compiled      Remove the compiled class file
  •   down                Put the application into maintenance mode
  •   env                 Display the current framework environment
  •   help                Displays help for a command
  •   inspire             Display an inspiring quote
  •   list                Lists commands
  •   migrate             Run the database migrations
  •   optimize            Optimize the framework for better performance
  •   serve               Serve the application on the PHP development server
  •   tinker              Interact with your application
  •   up                  Bring the application out of maintenance mode
16. Laravel 5 has built in CSRF protection on every route. How would I go about turning that off?
  Hopefully they mention that CSRF protection is important, but since we're asking them to turn it off they might not mention it which is OK. You'd easily turn it off by going into Kernel.php and removing it from the middleware stack that is ran on every route.

This is also a good place to ask about middleware in general and make sure they both understand what middleware is and why it's important.

17. List out some benefits of Laravel over other Php frameworks.
  • Setup and customization process is  easy and fast as compared to others.
  • Inbuilt Authentication System.
  • Supports multiple file systems
  • Pre-loaded packages like Laravel Socialite, Laravel cashier, Laravel elixir,Passport,Laravel Scout.
  • Eloquent ORM (Object Relation Mapping) with PHP active record implementation. 
  • Built in command line tool “Artisan” for creating a code skeleton ,database structure and build their migration.
Also Read: WordPress Interview Questions And Answers

18. What is composer ?
Composer is PHP dependency manager used for installing dependencies of PHP applications.

19. How to install laravel via composer ?

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

 20. How to check laravel current version ?
 You can check the current version of your Laravel installation using the --version option of artisan command Usages:
 [code] php artisan --version [/code]

21. What is php artisan. List out some artisan command ?

PHP artisan is the command line interface/tool included with Laravel. It provides a number of helpful commands that can help you while you build your application easily. Here are the list of some artisian command:
  • php artisan list
  • php artisan help
  • php artisan tinker
  • php artisan make
  • php artisan --versian
  • php artisan make modal modal_name
  • php artisan make controller controller_name 
22. Explain Events in laravel ?

An event is an incident or occurrence detected and handled by the program.Laravel event provides a simple observer implementation,that allow us  to subscribe and listen for events in our application.
Below are some events examples in laravel :
A new user has registered
A new comment is posted
User login/logout
New product is added.

23. How to enable query log in laravel?

Use the enableQueryLog method:
DB::connection()->enableQueryLog();
You can get array of the executed queries by using getQueryLog method:
$queries = DB::getQueryLog();

Also Read: Most Popular Codeigniter Interview Questions with Answers

24. How to turn off CRSF protection for a route in Laravel?

In "app/Http/Middleware/VerifyCsrfToken.php"
 //add an array of Routes to skip CSRF check
  private $exceptUrls = ['controller/route1', 'controller/route2'];
 //modify this function

[code]public function handle($request, Closure $next) {
 //add this condition foreach($this->exceptUrls as $route) {
 if ($request->is($route)) {
  return $next($request);
 }
}
return parent::handle($request, $next);
}[/code]

25. How can you get users IP address  in Laravel ?

[code] public function getUserIp(Request $request){
          // Getting ip address of remote user

            return $user_ip_address=$request->ip();
}[/code]

26. How to use custom table in Laravel Modal We can use custom table in laravel by overriding protected $table property of Eloquent. Below is sample uses

[code] class User extends Eloquent{
 protected $table="my_user_table";

}[/code]

27. How to define Fillable Attribute in Laravel Modal ?
You can define fillable attribute by overiding the fillable property of Laravel Eloquent. Here is sample uses

[code] Class User extends Eloquent{

protected $fillable =array('id','first_name','last_name','age');


}[/code]

28. In which directory controllers are located in Laravel ?

We kept all controllers in
app/http/Controllers directory

29. What does PHP compact function do ?
PHP compact function takes each key and tries to find a variable with that same name.If variable is found , them it builds an associative array.

30. Define ORM ?
Object-relational Mapping (ORM) is a programming technique for converting data between incompatible type systems in object-oriented programming languages.

Also Read: Apache Solr Interview Questions and Answers

31. How to create a record in Laravel using eloquent? 

To create a new record in the database using laravel Eloquent, simply create a new model instance, set attributes on the model, then call the save method: Here is sample Usage

[code] public function saveProduct(Request $request )
 $product = new product;
 $product->name = $request->name;
 $product->description = $request->name;
 $product->save(); [/code]

 32. What is the purpose of the Eloquent cursor() method in laravel?

The cursor method allows you to iterate through your database records using a cursor, which will only execute a single query. When processing large amounts of data, the cursor method may be used to greatly reduce your memory usage. Example Usage

[code] foreach (Product::where('name', 'bar')->cursor() as $flight) { //do some stuff } [/code]

33. How to get Logged in user info in laravel ?

Auth::User() function is used to get Logged in user info in laravel. Usage:

[code] if(Auth::check()){

$loggedIn_user=Auth::User();
dd($loggedIn_user);

} [/code]


34. What are Closures in laravel ?
Closures is an anonymous function that can be assigned to a variable or passed to another function as an argument.A Closures can access variables outside the scope that it was created.


3 comments:

  1. • Nice and good article. It is very useful for me to learn and understand easily. Thanks for sharing your valuable information and time. Please keep updatingAzure Online training Hyderabad

    ReplyDelete
  2. Pretty blog, so many ideas in a single site, thanks for the informative article, keep updating more article.
    PHP course in chennai

    ReplyDelete
  3. I think this is an informative post and it is very useful and knowledgeable. therefore, I would like to thank you for the efforts you have made in writing this article.website development agency

    ReplyDelete