Laravel Interview Questions – Set 04

What is the use of the Eloquent cursor() method in Laravel?

The cursor method allows us to iterate through our database using a cursor, which will only execute a single query. While processing large amounts of data, the cursor method may be used to reduce your memory usage greatly.

Example

foreach (Product::where(‘name’, ‘bar’)->cursor() as $flight) {
//make some stuff
}

What do you understand by Eloquent ORM?

Eloquent ORM (Object-Relational Mapping) is one of the main features of the Laravel framework. It may be defined as an advanced PHP implementation of the active record pattern.

Active record pattern is an architectural pattern which is found in software. It is responsible for keeping in-memory object data in relational databases

Eloquent ORM is also responsible for providing the internal methods at the same time when enforcing constraints on the relationship between database objects. Eloquent ORM represents database tables as classes, with their object instances tied to single table rows, while following the active record pattern.

How will you create a helper file in Laravel?

We can create a helper file using composer as per the given below steps:

Make a file “app/helpers.php” within the app folder.

Add

“files”: [
“app/helpers.php”
]
in the “autoload” variable.

Now update composer.json with composer dump-autoload or composer update.

How will you explain dd() function in Laravel?

dd stands for “Dump and Die.” Laravel’s dd() function can be defined as a helper function, which is used to dump a variable’s contents to the browser and prevent the further script execution.

Example

dd($array);

Which types of relationships are available in Laravel Eloquent?

Below are the types of relationships that Laravel Eloquent ORM supports:

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

What is a composer, and how can we install Laravel by the composer?

A composer is a dependency manager in PHP. It manages the dependencies which are required for a project. It means that the composer will pull in all the necessary libraries, dependencies, and manage all at a single place.

Laravel Installation Steps:

If you don’t have a composer on a system, download composer from https://getcomposer.org/download/
Open command prompt
Go to htdocs folder
Run the below command under C:xampphtdocs>

Explain the Service container and its advantages.

Service container in Laravel is one of the most powerful features. It is an important, powerful tool for resolving class dependencies and performing dependency injection in Laravel. It is also known as IoC container.

Dependency injection is a term which essentially means that class dependencies are “injected” into the class by the constructor or, in some cases,” setter” methods.

Advantages of Service Container

  • It can handle class dependencies on object creation.
  • It can combine interfaces to concrete classes.

How can we create a record in Laravel using eloquent?

We need to create a new model instance if we want to create a new record in the database using Laravel eloquent. Then we are required to set attributes on the model and call the save() method.

Example

public functionsaveProduct(Request $request )
$product = new product;
$product->name = $request->name;
$product->description = $request->name;
$product->save();

How can we get data between two dates using Query in Laravel?

We can use whereBetween() method to retrieve the data between two dates with Query.

Example

Blog::whereBetween(‘created_at’, [$date1, $date2])->get();

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