Laravel advanced interview questions

2024-03-19 07:43:37 Sanjeet Laravel
Laravel advanced interview questions

Advanced Laravel technical interview questions

We have listed some advanced Laravel interview Questions for experience Laravel programmers. If you are fresher then check our Basic Laravel interview Questions

1. Define Active Record Implementation. How to use it Laravel?

Active Record Implementation is an architectural pattern found in software engineering that stores in-memory object data in relational databases. Active Record facilitates the creation and use of business objects whose data is required to persistent in database. Laravel implements Active Records by Eloquent ORM. Below is sample usage of Active Records Implementation is Laravel.

$product = new Product;				
$product->title = 'Iphone 11';				
$product->save();

Active Record style ORMs map an object to a database row. In the above example, we would be mapping the Product object to a row in the products table of database.

2. Enlist different types of relationships supported by Laravel.

Laravel support 7 types of table relationships, they are

  • One To One
  • One To Many
  • One To Many (Inverse)
  • Many To Many
  • Has Many Through
  • Polymorphic Relations
  • Many To Many Polymorphic Relations

3. Explain Laravel Query Builder?

Laravel's database query builder provides a suitable, easy interface to creating and organization database queries. It can be used to achieve most database operations in our application and works on all supported database systems. The Laravel query planner uses PDO restriction necessary to keep our application against SQL injection attacks.

4. What is Laravel Elixir ?

Laravel Elixir provides a clean, fluent API for defining basic Gulp tasks for your Laravel application. Elixir supports common CSS and JavaScript preprocessors like Sass and Webpack. Using method chaining, Elixir allows you to fluently define your asset pipeline.

5. How to enable maintenance mode in Laravel?

You can enable maintenance mode in Laravel 5,simply by executing below command.

To enable maintenance mode

php artisan down

To disable maintenance mode

			
php artisan up

6. How to get current environment in Laravel 5?

You may access the current application environment via the environment method.

$environment = App::environment();

7. What is the purpose of using dd() function in Laravel?

Laravel's dd() is a helper function ,which will dump a variable’s contents to the browser and halt further script execution.

8. What is Method Spoofing in Laravel?

As HTML forms does not supports PUT, PATCH or DELETE request. So, when defining PUT, PATCH or DELETE routes that are called from an HTML form, you will need to add a hidden _method field to the form. The value sent with the _method field will be used as the HTTP request method:

<form action="/foo/bar" method="POST">
    <input type="hidden" name="_method" value="PUT">
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>

To generate the hidden input field _method, you may also use the method_field helper function:

<?php echo method_field('PUT'); ?>

In Blade template you can write it as below

{{ method_field('PUT') }}

9. What are Model Factories?

Model factories are a feature of laravel that allows you to build fake data for your models. Below is sample model factory code that you find in database/factories/ModelFactory.php.

$factory->define(App\User::class, function (Faker\Generator $faker) {
    return [
        'name' => $faker->name,
        'email' => $faker->email,
        'password' => bcrypt(str_random(10)),
        'remember_token' => str_random(10),
    ];
});

10. What are Accessors and Mutators in Eloquent?

Accessors and mutators are custom, user defined methods that allow you to format Eloquent attributes. Accessors are used to format attributes when you retrieve them from the database, while mutators format the attributes before saving them to the database.

11. How do you register a middleware in Laravel?

You can register your middlewares in 3 ways in Laravel

  1. As Global Middleware
  2. Assign it to your Routes
  3. Add it in Middleware Groups

12. What are terminable Middleware in Laravel?

Terminable middleware performs some task after the response has been sent to the browser. This can be accomplished by creating a middleware with terminate method in the middleware. Terminable middleware should be registered with global middleware. The terminate method will receive two arguments $request and $response.

This post is submitted by one of our members. You may submit a new post here.

Related Tricks