Q1. What is Laravel?
Laravel is a free and open-source PHP framework for building web applications. If you are familiar with Core PHP and Advanced PHP, Laravel will make your task easier. It saves a lot of time if you are planning to develop a website from scratch. Moreover, a website built in Laravel is secure and prevents several web attacks.
Q2. What are some benefits of Laravel over other PHP frameworks?
There are a variety of tools and frameworks available to you when building a web application. However, we believe Laravel is the best choice for building modern, full-stack web applications.
Laravel offers you the following advantages when you are designing a web application based on it −
- The 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, and Passport.
- Eloquent ORM(Object Relation Mapping) with PHP active record implementation.
- Build in command line tool "Artisan" for creating a code skeleton, and database structure, and build their migration.
Q3. What is Artisan in Laravel?
Artisan is the command line interface that comes with Laravel. It is Laravel's own Command Line interface. It's like a Linux command line but Its commands are helpful for building a Laravel application. With this command-line tool, we can make models, and controllers, and can do data migrations, and many more.
Here is a list of few commands in Artisan along with their respective functionalities −
- php artisan list: view the list of available commands supported by Artisan
- php artisan serve: start the Laravel project
- php artisan down: in maintenance mode
- php artisan up: out maintenance mode
- php artisan -V: version of our Laravel app
- php artisan make:controller controller_name: create a controller
Q4. What is Tinker in Laravel?
Laravel Tinker is a powerful REPL(Read–Eval–Print-Loop) for the Laravel framework, powered by the PsySH package. Tinker allows you to interact with your entire Laravel application on the command line, including your Eloquent models, jobs, events, and more.
To enter the Tinker environment, run the tinker Artisan command:
php artisan tinkerQ5. Explain the migrations in Laravel.
Laravel Migration is an essential feature in Laravel that allows you to create a table in your database. It allows you to modify and share the application's database schema. You can modify the table by adding a new column or deleting an existing column. Laravel Migration allows you to add a new column or delete the records in your database without deleting the records that are already present.
Create Migrations:
php artisan make:migration create_tablename_table
EX: php artisan make:migration create_students_table
Other Migration Commands
- php artisan migrate
- php artisan migrate:fresh -> drop all the tables from the database, and then it re-runs all the migrations.
- php artisan migrate:refresh -> rollback all the migrations and then re-run the migrations
- php artisan migrate:reset -> rollback all the migrations
- php artisan migrate:rollback -> rollback the last database migration
- php artisan migrate:status -> show the status of each migration.
- php artisan migrate:install -> creates the migration table in a database.
Q6. What do you know about query builder in Laravel?
In Laravel, the database query builder provides an easy interface to create and run database queries. It can be used to perform all the database operations in your application, from basic DB Connection, CRUD, Aggregates, etc. and it works on all supported database systems like a SQL, Mongodb.
The Laravel query builder uses PDO parameter binding to protect your application against SQL injection attacks.
Here we give you some quires of query builder:
$users = DB::table('users')->get();
$users = DB::table('users')->where('name', 'ram')->first();
$users = DB::table('users')->where('name', 'ram')->pluck('id');
Q7. What is eager loading and when do you use it?
Eager Loading means you get all of the required data at the same time. Eager loading helps us nicely solve the N+1 query problem, where N is the number of items being fetched from the database. Laravel provides a with() method for loading relationship records with main query and archive eager loading.
Imagine that you have two models (Post and Author) with a one-to-one relationship between them. Now imagine that you have 100 posts and you want to loop through each one of them and output the author’s name.
Without eager loading, your code might look like this:
$posts = Post::all();foreach ($posts as $post ) {print_r($post->author->name);}
The code above would result in 101 database queries because the results are "lazy loaded"! The first query would be to fetch all of the posts. The other one hundred queries would come from getting the author’s name in each iteration of the loop. Obviously, this can cause performance issues and slow down your application. So, how would we improve this?
As you can see, this code looks almost the same and is still readable. By adding the ::with('authors') this will fetch all of the comments and then make another query to fetch the authors at once. So, this means that we will have cut down the query from 101 to 2.$posts = Post::with(‘authors’)->get();foreach ($posts as $post ) {print_r($post->author->name);}
Q8: Difference between eager and lazy loading
| Eager Loading | Lazy Loading |
|---|---|
| Eager loading gets all data with relationship records in single query | lazy loading require N+1 queries for getting the main model and related data. |
| Eager loading run single query | lazy loading runs N+1 queries. |
| Eager loading execution speed is fast and takes less memory compared to lazy loading. | Lazy loading execution speed slows and takes more memory compare to eager loading. |
$posts = Post::with(‘comments’)->get(); | $posts = Post::all(); $comments=$post->comments()->get() |
Q9. What is the service container?
A service container is a powerful tool for managing class dependencies and performing dependency injection. When you create any Service in laravel app and want to use it anywhere in the application then simply you can type hint the service class in constructor or method, then service container automatically resolved this dependency and gives the instance of the injected class (type hinted class or service) which you can use within the class or constructor.
Q10. What is the Service Provider?
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. You can also register your services in providers using bind or singleton within the register method of the service provider.
Laravel provides some default service provider which is presented in the app\Providers directory like AppServiceProvider, AuthServiceProvider, and RouteServiceProvider, and these all service providers are registered in the providers array within config\app.php file.
You can also make a custom service provider and then register in the config\app.php file and use it anywhere in your Laravel application. Given below command is used for creating a service provider.
php artisan make:provider CustomServiceProviderService Provider gives you two methods one register() and another one is boot() method. In the register method, you can register your services, listeners, and middleware.
Q11. What is the Deferred Service Provider?
Deferred service providers are the providers which will not be loaded from the filesystem on every request, they only load when the services they provide are actually needed. So, It will improve the performance of your application.
For creating a provider as deffer then, implement the \Illuminate\Contracts\Support\DeferrableProvider interface and define a provides method. The provides method should return the service container bindings registered by the provider: