InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 1. |
What are Models? |
|
Answer» With LARAVEL, each DATABASE table can have a model representation using a model file which can be used to interact with that table using Laravel Eloquent ORM. We can create a model using this ARTISAN command: php artisan make:model Post This will create a file in the models’ directory and will look like below: class Post EXTENDS Model{ /** * The attributes that are mass assignable. * * @var array */ PROTECTED $fillable = []; /** * The attributes that should be hidden for arrays. * * @var array */ protected $hidden = [];}A Model can have properties like table, fillable, hidden, etc which defines properties of the table and model. |
|
| 2. |
How to implement soft delete in Laravel? |
|
Answer» Soft Delete MEANS when any data row is deleted by any means in the DATABASE, we are not deleting the data but adding a timestamp of deletion. We can add soft delete FEATURES by adding a trait in the model FILE like below. USE Illuminate\Database\Eloquent\Model;use Illuminate\Database\Eloquent\SoftDeletes;class Post extends Model { use SoftDeletes; protected $table = 'posts'; // ...} |
|
| 3. |
What are factories in Laravel? |
|
Answer» Factories are a way to put values in FIELDS of a particular model automatically. Like, for testing when we add multiple fake records in the database, we can use factories to generate a class for each model and put data in fields accordingly. Every new laravel application comes with database/factories/UserFactory.php which looks like below: <?phpnamespace Database\Factories;use App\Models\User;use Illuminate\Database\Eloquent\Factories\Factory;use Illuminate\Support\Str;class UserFactory extends Factory{ /** * The name of the factory's corresponding model. * * @VAR string */ protected $model = User::class; /** * Define the model's default STATE. * * @return array */ public function definition() { return [ 'name' => $this->faker->name, 'email' => $this->faker->unique()->safeEmail, 'email_verified_at' => now(), 'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password 'remember_token' => Str::random(10), ]; }}We can CREATE a new factory using php artisan make:factory UserFactory --class=User. The above command will create a new factory class for the User model. It is just a class that extends the base Factory class and makes use of the Faker class to generate fake data for each column. With the COMBINATION of factory and seeders, we can easily add fake data into the database for testing purposes. |
|
| 4. |
What are seeders in Laravel? |
|
Answer» Seeders in Laravel are used to put data in the database tables AUTOMATICALLY. After running migrations to CREATE the tables, we can run `php artisan db:seed` to run the seeder to populate the database tables. We can create a new Seeder using the below artisan COMMAND: php artisan make:seeder [className] It will create a new Seeder like below: <?phpuse App\Models\Auth\User;use Illuminate\Database\Eloquent\Model;use Illuminate\Database\Seeder;class UserTableSeeder extends Seeder{ /** * Run the database seeds. */ public function run() { factory(User::class, 10)->create(); }}The run() method in the above CODE snippet will create 10 new users using the User factory. Factories will be explained in the next question. |
|
| 5. |
What are migrations in Laravel? |
|
Answer» In simple, Migrations are used to create database schemas in Laravel. In migration files, we store which table to create, update or delete. Each migration file is stored with its timestamp of creation to keep track of the order in which it was created. As migrations go up with your code in GitHub, GITLAB, etc, whenever ANYONE clones your project they can run `PHP artisan migrate` to run those migrations to create the database in their environment. A normal migration file looks LIKE below: <?phpuse Illuminate\Database\Migrations\Migration;use Illuminate\Database\Schema\Blueprint;use Illuminate\Support\Facades\Schema;class CreateUsersTable extends Migration{ /** * Run the migrations. * * @RETURN void */ public function up() { Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('name'); // Create other columns }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('users'); }}The up() method runs when we run `php artisan migrate` and down() method runs when we run `php artisan migrate:rollback`. If we rollback, it only rolls back the previously run migration. If we want to rollback all migrations, we can run 'php artisan migrate:reset`. If we want to rollback and run migrations, we can run `PHP artisan migrate:refresh`, and we can use `PHP artisan migrate:fresh` to drop the tables first and then run migrations from the start. |
|
| 6. |
What are the default route files in Laravel? |
|
Answer» Below are the four default route FILES in the routes folder in Laravel:
|
|
| 7. |
How to put Laravel applications in maintenance mode? |
|
Answer» Maintenance mode is used to put a maintenance page to customers and under the hood, we can do software updates, BUG fixes, ETC. Laravel applications can be put into maintenance mode USING the below command: php artisan down And can put the application again on live using the below command: php artisan up Also, it is POSSIBLE to access the website in maintenance mode by whitelisting particular IPs. |
|
| 8. |
Can we use Laravel for Full Stack Development (Frontend + Backend)? |
|
Answer» LARAVEL is the best choice to make progressive, scalable full-stack web applications. Full-stack web applications can have a BACKEND in laravel and the frontend can be MADE using BLADE files or SPAs using Vue.js as it is provided by default. But it can also be used to just provide rest APIs to a SPA application. Hence, Laravel can be used to make full-stack applications or just the backend APIs only. |
|
| 9. |
How to define environment variables in Laravel? |
|
Answer» The environment variables can be defined in the .env FILE in the project directory. A brand new laravel application COMES with a .env.example and while INSTALLING we copy this file and rename it to .env and all the environment variables will be defined here. Some of the EXAMPLES of environment variables are APP_ENV, DB_HOST, DB_PORT, etc. |
|
| 10. |
What is an artisan? |
|
Answer» Artisan is the command-line tool for Laravel to help the developer build the application. You can enter the below command to GET all the available commands: PHP artisan list: Artisan command can help in creating the FILES using the make command. Some of the USEFUL make commands are listed below: php artisan make:controller - Make Controller file php artisan make:model - Make a Model file php artisan make:migration - Make Migration file php artisan make:seeder - Make Seeder file php artisan make:factory - Make Factory file php artisan make:policy - Make Policy file php artisan make:command - Make a new artisan command |
|
| 11. |
What are available databases supported by Laravel? |
|
Answer» The SUPPORTED DATABASES in LARAVEL are:
|
|
| 12. |
What is the templating engine used in Laravel? |
|
Answer» The templating engine USED in Laravel is Blade. The blade gives the ability to USE its mustache-like syntax with the PLAIN PHP and GETS COMPILED into plain PHP and cached until any other change happens in the blade file. The blade file has .blade.php extension. |
|
| 13. |
Define Composer. |
|
Answer» COMPOSER is the package manager for the FRAMEWORK. It helps in adding new packages from the huge community into your laravel application. For example, one of the most used packages for authentication will be Passport, for including that into your project, you can run the below command on your terminal: composer requires laravel/passportIt GENERATES a file(composer.json) in your project directory to keep track of all your packages. A default composer.json file of your laravel project will look something like below: { "name": "laravel/laravel", "type": "project", "DESCRIPTION": "The Laravel Framework.", "keywords": [ "framework", "laravel" ], "license": "MIT", "require": { "php": "^7.3|^8.0", "fideloper/proxy": "^4.4", "fruitcake/laravel-cors": "^2.0", "guzzlehttp/guzzle": "^7.0.1", "laravel/framework": "^8.12", "laravel/tinker": "^2.5" }, "require-dev": { "facade/ignition": "^2.5", "fakerphp/faker": "^1.9.1", "laravel/sail": "^1.0.1", "mockery/mockery": "^1.4.2", "nunomaduro/collision": "^5.0", "phpunit/phpunit": "^9.3.3" }}The “require” and “require-dev” keys in composer.json specify production and dev packages and their version constraints respectively. |
|