Laravel Interview Questions – Set 01

What is Query Builder in Laravel?

Laravel’s Query Builder provides more direct access to the database, alternative to the Eloquent ORM. It doesn’t require SQL queries to be written directly. Instead, it offers a set of classes and methods which are capable of building queries programmatically. It also allows specific caching of the results of the executed queries.

What are the requirements for Laravel 5.8?

  • PHP Version>=7.1.3
  • OpenSSL PHP Extension
  • PDO PHP Extension
  • Mbstring PHP Extension
  • Tokenizer PHP Extension
  • XML PHP Extension
  • Ctype PHP Extension
  • JSON PHP Extension

What do you know about PHP artisan? Mention some artisan command.

PHP artisan is a command-line interface/tool provided with Laravel. It consists of several useful commands which can be helpful while building an application. There are few artisan commands given below:

PHP artisan list

A ‘list’ command is used to view a list of all available Artisan commands.

PHP artisan help

Every command also contains a ‘help’ screen, which is used to display and describe the command’s available arguments and options. To display a help screen, run ‘help’ command.

PHP artisan tinker

Laravel’s artisan tinker is a repl (Read-Eval-Print Loop). Using tinker, one can write actual PHP code through command-line. One can even update or delete table records in the database.

PHP artisan -version

By using this command, one can view the current version of Laravel installation.

PHP artisan make model model_name

This command creates a model ‘model_name.php’ under the ‘app’ directory.

PHP artisan make controller controller_name

This command is used to build a new controller file in app/Http/Controllers folder.

What do you understand by ORM?

ORM stands for Object-Relational Mapping. It is a programming technique which is used to convert data between incompatible type systems in object-oriented programming languages.

Does Laravel support caching?

Yes, Laravel provides support for popular caching backends like Memcached and Redis.

By default, Laravel is configured to use file cache driver, which is used to store the serialized or cached objects in the file system. For huge projects, it is suggested to use Memcached or Redis.

What do you know about Laravel Contracts?

Laravel’s Contracts are the set of interfaces which are responsible for defining the core functionality of services provided by the Laravel framework.

How can we check the logged-in user info in Laravel?

User() function is used to get the logged-in user

Example

if(Auth::check()){
$loggedIn_user=Auth::User();
dd($loggedIn_user);
}

What do you know about CSRF token in Laravel? How can someone turn off CSRF protection for a specific route?

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.

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 appHttpMiddlewareVerifyCsrfToken.phpfile.

Example

classVerifyCsrfToken extends BaseVerifier
{
protected $except = [
‘Pass here your URL’,
];
}

List some official packages provided by Laravel?

There are some official packages provided by Laravel which are given below:

Cashier

Laravel cashier implements an expressive, fluent interface to Stripe’s and Braintree’s subscription billing services. It controls almost all of the boilerplate subscription billing code you are dreading writing. Moreover, the cashier can also control coupons, subscription quantities, swapping subscription, cancellation grace periods, and even generate invoice PDFs.

Envoy

Laravel Envoy is responsible for providing a clean, minimal syntax for defining frequent tasks that we run on our remote servers. Using Blade style syntax, one can quickly arrange tasks for deployment, Artisan commands, and more. Envoy only provides support for Mac and Linux.

Passport

Laravel is used to create API authentication to act as a breeze with the help of Laravel passport. It further provides a full Oauth2 server implementation for Laravel application in a matter of minutes. Passport is usually assembled on top of League OAuth2 server which is maintained by Alex Bilbie.

Scout

Laravel Scout is used for providing a simple, driver-based solution for adding full-text search to the eloquent models. Using model observers, Scout automatically keeps search indexes in sync with eloquent records.

Socialite

Laravel Socialite is used for providing an expressive, fluent interface to OAuth authentication with Facebook, Twitter, Google, and Linkedln, etc. It controls almost all the boilerplate social authentication code that you are dreading writing.

In which directory controllers are kept in Laravel?

Controllers are kept in app/http/Controllers directory.