Laravel interview questions

Laravel interview questions

Laravel is a Symfony based free open-source PHP web framework. It is created by Taylor Otwell and allows developers to write expressive, elegant syntax. Laravel comes with built-in support for user authentication and authorization which is missing in some most popular PHP frameworks like CodeIgniter, CakePHP.

Get Ultimate Laravel Hosting

Points to be remembered while appearing in Laravel Interviews :

  • Laravel is developed on the MVC (Model-View-Controller) design pattern.
  • Laravel comes with inbuilt features/ modules like authentication, authorization, localization, models, views, sessions, paginations and routing
  • Laravel supports advanced concepts of PHP and OOPs like Dependency Injection, traits, Contracts, bundles, Namespaces, Facades
  • Laravel supports Multiple Databases like MySQL, PostgreSQL, SQLite, SQL Server.
  • Laravel allows developers to write a clean and modular code.
  • Laravel supports blade Template Engine
  • Laravel comes with Official Packages like Cashier, Envoy, Horizon, Passport, Scout, Socialite
  • Laravel can be used with various popular Javascript Frameworks Like AngularJs, VueJs, ReactJS.
Download Laravel interview questions PDF

Laravel interview questions

You can use custom table in Laravel by overriding protected $table property of Eloquent.


Below is sample uses

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

} 

In Laravel, bundles are also called packages.Packages are the primary way to extend the functionality of Laravel. Packages might be anything from a great way to work with dates like Carbon, or an entire BDD testing framework like Behat.In Laravel, you can create your custom packages too.You can read more about packages from here

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.

Laravel reverse routing is generating URL's based on route declarations. Reverse routing makes your application so much more flexible. It defines a relationship between links and Laravel routes. When a link is created by using names of existing routes, appropriate Uri's are created automatically by Laravel. Here is an example of reverse routing.

// route declaration

Route::get(‘login’, ‘users@login’);

Using reverse routing we can create a link to it and pass in any parameters that we have defined. Optional parameters, if not supplied, are removed from the generated link.

{{ HTML::link_to_action('users@login') }}

It will automatically generate an Url like http://xyz.com/login in view.

To turn off CRSF protection in Laravel add following codes in “app/Http/Middleware/VerifyCsrfToken.php”

 
//add an array of Routes to skip CSRF check
private $exceptUrls = ['controller/route1', 'controller/route2'];
 //modify this function
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);
} 

PHP Traits are simply a group of methods that you want include within another class. A Trait, like an abstract class cannot be instantiated by itself.Trait are created to reduce the limitations of single inheritance in PHP by enabling a developer to reuse sets of methods freely in several independent classes living in different class hierarchies.

Here is an example of trait.

trait Sharable {
 
  public function share($item)
  {
    return 'share this item';
  }
 
}

You could then include this Trait within other classes like this:


class Post {
 
  use Sharable;
 
}
 
class Comment {
 
  use Sharable;
 
}

Now if you were to create new objects out of these classes you would find that they both have the share() method available:


$post = new Post;
echo $post->share(''); // 'share this item' 
 
$comment = new Comment;
echo $comment->share(''); // 'share this item'

Laravel's Eloquent ORM is simple Active Record implementation for working with your database. Laravel provide many different ways to interact with your database, Eloquent is most notable of them. Each database table has a corresponding “Model” which is used to interact with that table. Models allow you to query for data in your tables, as well as insert new records into the table.

Below is sample usage for querying and inserting new records in Database with Eloquent.


// Querying or finding records from products table where tag is 'new'
$products= Product::where('tag','new');
// Inserting new record 
 $product =new Product;
 $product->title="Iphone 7";
 $product->price="$700";
 $product->tag='iphone';
 $product->save();

Laravel Facades provides a static like an interface to classes that are available in the application’s service container. Laravel self-ships with many facades which provide access to almost all features of Laravel ’s. Laravel facades serve as “static proxies” to underlying classes in the service container and provide benefits of a terse, expressive syntax while maintaining more testability and flexibility than traditional static methods of classes. All of Laravel’s facades are defined in the Illuminate\Support\Facades namespace. You can easily access a facade like so:


use Illuminate\Support\Facades\Cache;

Route::get('/cache', function () {
    return Cache::get('key');
});

Laravel's Contracts are nothing but a set of interfaces that define the core services provided by the Laravel framework.
Read more about laravel Contract’s

Service Providers are central place where all laravel application is bootstrapped . Your application as well all Laravel core services are also bootstrapped by service providers.
All service providers extend the Illuminate\Support\ServiceProvider class. Most service providers contain a register and a boot method. Within the register method, you should only bind things into the service container. You should never attempt to register any event listeners, routes, or any other piece of functionality within the register method.
You can read more about service provider from here

Below are list of some official/ default packages provided by Laravel 5.6

  • Cashier
  • Envoy
  • Passport
  • Scout
  • Socialite
  • Horizon

Laravel 5.0 features

In Programming validations are a handy way to ensure that your data is always in a clean and expected format before it gets into your database. Laravel provides several different ways to validate your application incoming data.By default Laravel’s base controller class uses a ValidatesRequests trait which provides a convenient method to validate all incoming HTTP requests coming from client.You can also validate data in laravel by creating Form Request.
click here read more about data validations in Laravel.

An event is an action or occurrence recognized by a program that may be handled by the program or code. Laravel events provides a simple observer implementation, that allowing you to subscribe and listen for various events/actions that occur in your application.

All Event classes are generally stored in the app/Events directory, while their listeners are stored in app/Listeners of your application.

You can install Laravel via composer by running below command.

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

Also Read Core PHP Interview Questions and Answers for 2019