Laravel Interview Questions – Set 03

How can someone change the default database type in Laravel?

Laravel is configured to use MySQL by default.

To change its default database type, edit the file config/database.php:

  • Search for ‘default’ =>env(‘DB_CONNECTION’, ‘mysql’)
  • Change it to whatever required like ‘default’ =>env(‘DB_CONNECTION’, ‘sqlite’)
    By using it, MySQL changes to SQLite.

What do you understand by database migrations in Laravel? How can we use it?

Migrations can be defined as version control for the database, which allows us to modify and share the application’s database schema easily. Migrations are commonly paired with Laravel’s schema builder to build the application’s database schema easily.

A migration file includes two methods, up() and down(). A method up() is used to add new tables, columns or indexes database and the down() method is used to reverse the operations performed by the up() method.

We can generate a migration and its file by using the make:migration.

Syntax

php artisan make:migration blog
By using it, a current date blog.php file will be created in database/migrations

How can we use the custom table in Laravel?

We can easily use custom table in Laravel by overriding protected $table property of Eloquent. Here, is the sample:

class User extends Eloquent{
protected $table=”my_user_table”;
}

What are the main features of Laravel?

Some of the main features of Laravel are:

  • Eloquent ORM
  • Query builder
  • Reverse Routing
  • Restful Controllers
  • Migrations
  • Database Seeding
  • Unit Testing
  • Homestead

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.

Php artisan -version

Explain some benefits of Laravel over other PHP frameworks.

There are few benefits of Laravel which can be considered over other PHP frameworks:

  • In Laravel, Setup and customization process is fast and easy as compared to others.
  • Laravel supports multiple file systems.
  • It has pre-loaded packages like Laravel Socialite, Laravel cashier, Laravel Passport, Laravel elixir, and Laravel Scout, etc.
  • It consists of in-built Authentication System.
  • It supports Eloquent ORM (Object Relation Mapping) with PHP active record implementation.
  • “Artisan” command-line tool for creating a database structure, code skeleton, and build their migration.

How will you describe Bundles in Laravel?

In Laravel, Bundles are also known as Packages. Packages are the primary way to add more functionality to Laravel. Packages can be anything, from a great way to work with dates like Carbon, or an entire BDD testing framework like Behat. Laravel also provides support for creating custom packages.

There are different types of packages. Some of them are stand-alone packages. This means they can work with any PHP framework. The frameworks like Carbon and Behat are examples of stand-alone packages. Other packages are intended for use with Laravel. These packages may contain routes, controllers, views, and configurations which are mainly designed to enhance a Laravel application.

Which template engine is used by Laravel?

The blade is a simple but powerful templating engine provided with Laravel. There is no restriction to use PHP codes in the views. All the blade views are compiled into simple PHP code and cached until they are modified. Blade adds effectively zero overhead to our application. Blade view files in Laravel use the .blade.phpfile extension and are saved in the resources/views directory.

How can we use maintenance mode in Laravel 5?

When an application is in maintenance mode, a custom view is displayed for all requests into the application. It makes it easy to “disable” application while it is updating or performing maintenance. A maintenance mode check is added in the default middleware stack for our application. When an application is in maintenance mode, a MaintenanceModeException will be thrown with a status code of 503.

We can enable or disable maintenance mode in Laravel 5, simply by executing the below command:

// Enable maintenance mode
php artisan down

// Disable maintenance mode
php artisan up

What do you know about Service providers in Laravel?

Service providers can be defined as the central place to configure all the entire Laravel applications. Applications, as well as Laravel’s core services, are bootstrapped via service providers. These are powerful tools for maintaining class dependencies and performing dependency injection. Service providers also instruct Laravel to bind various components into the Laravel’s Service Container.

An artisan command is given here which can be used to generate a service provider:

php artisan make: provider ClientsServiceProvider
Almost, all the service providers extend the IlluminateSupportServiceProviderclass. Most of the service providers contain below-listed functions in its file:

Register() Function
Boot() Function
Within the Register() method, one should only bind things into the service container. One should never attempt to register any event listeners, routes, or any other piece of functionality within the Register() method.