Laravel Framework MCQs

Laravel Framework MCQs

These Laravel Framework multiple-choice questions and their answers will help you strengthen your grip on the subject of Laravel Framework. You can prepare for an upcoming exam or job interview with these 100+ Laravel Framework MCQs.
So scroll down and start answering.

1:

To validate a date named finish_date against following rules: -Should be date -Required -Should be greater than start_date which of the following is correct validation rule?

A.   'finish_date' => 'required|date|after|start_date'

B.   'finish_date' => 'required|date|>:start_date'

C.   'finish_date' => 'required|date|after:start_date'

D.   'finish_date' => 'required|date|greater:start_date'

2:

To protect application from cross-site request forgery attacks, which of the following should be included in forms?

A.   {{ secure }}

B.   {{ csrf_field() }}

C.   {{ protect() }}

D.   {{ csrf_protect() }}

3:

Validation If checkbox ticked then Input text is required?

A.   return [ 'has_login' => 'sometimes', 'pin' => 'required_with:has_login,on', ];

B.   return [ 'has_login' => 'sometimes', 'pin' => 'required_if:has_login,on', ];

C.   return [ 'has_login' => 'accepted', 'pin' => 'required', ];

D.   All of the above

4:

____ are an important part of any web-based application. They help control the flow of the application which allows us to receive input from our users and make decisions that affect the functionality of our applications.

A.   Routing

B.   Module

C.   Views

D.   Forms

5:

___ web applications respond to meaningful HTTP verbs with appropriate data.

A.   Validation

B.   RESTful

C.   Database

D.   Views

6:

How to use laravel routing for unknown number of parameters in URL.

A.   //in routes.php Route::Itemget('/{book?}/{chapter?}/{topic?}/{article?}/{sublevels?}', 'controller@func')->where('sublevels', '.*'); //in your controller public function func($book = null, $chapter = null, $topic = null, $article = null, $sublevels = null) { //this will give you the array of sublevels if (!empty($sublevels) $sublevels = explode('/', $sublevels); ... }

B.   //in routes.php Route::get('/{book?}/{chapter?}/{topic?}/{article?}/{sublevels?}', 'controller@func')->where('sublevels', '.*'); //in your controller public function func($book = null, $chapter = null, $topic = null, $article = null, $sublevels = null) { //this will give you the array of sublevels if (!empty($sublevels) $sublevels = explode('/', $sublevels); ... }

C.   //in routes.php Route::get('/{book?}/{chapter?}/{topic?}/{article?}', 'controller@func'); //in your controller public function func($book = null, $chapter = null, $topic = null, $article = null) { ... }

D.   //in routes.php Route::Itemget('/{book?}/{chapter?}/{topic?}/{article?}', 'controller@func'); //in your controller public function func($book = null, $chapter = null, $topic = null, $article = null) { ... }

7:

How access custom textbox name in Laravel using validation 'unique'?

A.   'admin_username' => 'required|min:5|max:15|unique:administrators,username',

B.   'admin_username' => 'required|min:5|max:15|unique:administrators',

C.   'admin_username' => 'requireds|min:5|max:15|unique:administrators'.username',

D.   All of the above

8:

The field under validation must be present in the input data and not empty. A field is considered "empty" if one of the following conditions are true:

A.   The value is null.

B.   The value is an empty string.

C.   The value is an empty array or empty Countable object.

D.   The value is an uploaded file with no path.

E.   All of the above

9:

How do you define a single action controller for the following route?

Route::get('user/{id}', 'ShowProfile');

A.   You may place a single __construct method on the controller

B.   You may place a single __invoke method on the controller

C.   You may place a single __create method on the controller

D.   You can not create single action controller in laravel

10:

_____ directory contains much of the custom code used to power your application, including the models, controllers and middleware.

A.   app

B.   view

C.   controller

D.   models

11:

If you need to add additional routes to a resource controller beyond the default set of resource routes, you should define those routes after your call to Route::resource

A.   True

B.   False

12:

Which of the following validation rules are acceptable?

A.   ['field' => 'accepted']

B.   ['field' => 'active_url']

C.   ['field' => 'after:10/10/16']

D.   ['field' => 'alpha']

13:

Which of the following is the correct way to set SQLite as database for unit testing?

A.   'sqlite' => [ 'driver' => 'sqlite', 'database' => storage_path.'/database.sqlite', 'prefix' => '', ],

B.   'sqlite' => [ 'driver' => 'sqlite', 'database' => storage_path('database.sqlite'), 'prefix' => '', ],

C.   'sqlite' => [ 'driver' => 'sqlite', 'dtabase' => storage_path().'/database.sqlite', 'prefix' => '', ],

D.   All of the above

14:

Which method can be used to create custom blade directives?

A.   Blade::directive()

B.   Blade::custom()

C.   We can not create custom blade commands

15:

Which of the following is correct to have an optional parameter in route?

A.   Route::get('user/{name}', function ($name = null) { return $name; });

B.   Route::get('user/{name?}', function ($name = null) { return $name; });

C.   Route::get('user/{id}', function ($name = null) { return $name; });

D.   Route::get('user/id?', function ($name = null) { return $name; });

16:

Which of the following is correct to create a route(s) to resource controller named "PostController"?

A.   Route::resource('PostController', 'post');

B.   Route::resource('post', 'PostController');

C.   Route::get('post', 'PostController');

D.   Route::post('post', 'PostController');

17:

Which of the following methods can be used to register a route that responds to all HTTP verbs?

A.   Route::any()

B.   Route::match()

C.   Route::all()

18:

How the Laravel controller method get the parameters?

A.   public function store(Request $request, $foo = 'bar'){ { $this->validate($request, [ 'title' => 'required|unique|max:255', 'body' => 'required', ]); }

B.   public function store(Request $request) { $this->validate($request, [ 'title' => 'required|unique|max:255', 'body' => 'required', ]); }

C.   public function store($foo = 'bar', Request $request){ { $this->validate($request, [ 'title' => 'required|unique|max:255', 'body' => 'required', ]); }

D.   All of the above

19:

Which of the following is correct to define middleware for multiple views in controller file?

A.   public function __construct() { $this->middleware('admin', ['only' => ['create', 'edit', 'show']]); }

B.   public function __construct() { $this->middleware('admin', ['only' => 'create|edit|show']);

C.   public function __construct() { $this->middleware('admin', ['only' => 'create']); }

D.   All of the above

20:

Which of the following is/are correct to define custom validation messages?

A.   Pass the custom messages as the third argument to the Validator::make method

B.   Specify your custom messages in a language file.

C.   Customize the error messages used by the form request by overriding the messages method

21:

Use a validation rules inside the custom validation class?

A.   $emailsOutput = Output::get('email'); $emails = explode(',', $emails); foreach($emails as $email) { $validator = Validator::make( ['email' => $email], ['email' => 'required|email'] ); if ($validator->fails()) { // There is an invalid email in the input. } }

B.   $emailsInput = Input::get('email'); $emails = explode(',', $emails); foreach($emails as $email) { $validator = Validator::make( ['email' => $email], ['email' => 'required|email'] ); if ($validator->fails()) { // There is an invalid email in the input

C.   $emailsOutput = Output::get_email('email'); $emails = explode(',', $emails); foreach($emails as $email) { $validator = Validator::make( ['email' => $email], ['email' => 'required|email'] ); if ($validator->fails()) { // There is an invalid email in the input. } }

D.   None

22:

Which of the following methods can be used with models to build relationships?

A.   belongsToMany()

B.   hasOne()

C.   hasMany()

D.   belongsTo()

23:

Which of the following can be used to create a controller with typical "CRUD" routes?

A.   php artisan make:controller YourController

B.   php artisan make:controller YourController --crud

C.   php artisan make:controller YourController –resource

D.   php artisan make:controller YourController –all

24:

To create a controller which handles all "CRUD" routes, which of the following is correct command?

A.   php artisan make:controller CarController --all

B.   php artisan make:controller CarController --crud

C.   php artisan make:controller CarController

D.   php artisan make:controller CarController –resource

25:

If blog post has infinite number of comments we can define one-to-many relationship by placing following code in post model?

A.   public function comments() { return $this->hasMany('App\Comment'); }

B.   public function comments() { return $this->belongsTo('App\Comment'); }

C.   public function comments() { return $this->oneToMany('App\Comment'); }

26:

Which of the following variable is available inside your loops in blade?

A.   $iterate

B.   $first

C.   $index

D.   $loop

27:

Which of the following controller should be extended by all controllers?

A.   Add Controller

B.   Advance Controller

C.   Base Controller

D.   View Controller

28:

Which view facade within a service provider's boot method can be used to share data with all views?

A.   View::config('key', 'value');

B.   View::put('key', 'value');

C.   View::store('key', 'value');

D.   View::share('key', 'value');

29:

List out few different return types of a controller action method?

A.   View Result, Javascript Result

B.   Redirect Result, Json Result

C.   Content Result

D.   All of the above

30:

Which method can be used to store items in cache permanently?

A.   Cache::permanent('key', 'value');

B.   Cache::add('key', 'value');

C.   Cache::put('key', 'value');

D.   Cache::forever('key', 'value');

31:

\Config::set('database.connections.customers.database', 'db_A'); 

A.   DB::reconnect('customers');

B.   DB::unprepared('USE db_A');

C.   DB::select('SELECT DATABASE()')[0]->{"DATABASE()"}

D.   All of the above

32:

Which of the following are correct for route definitions?

A.   Route::get('user/{name?}')

B.   Route::get('user/{name}')

C.   Route::get('{user}/name')

D.   Route::get('user/?name}')

33:

How to get Header Authorization key in controller?

A.   public function yourControllerFunction(\Illuminate\Http\Request $request) { $header = $request->addheader('Authorization'); // do some stuff }

B.   public function yourControllerFunction(\Illuminate\Http\Request $request) { $header = $request->header('Authorization'); // do some stuff }

C.   public function yourControllerFunction(/Illuminate/Http/Request) { $header = $request->addheader('Authorization'); // do some stuff }

D.   None of the above

34:

To share route attributes, it is best to use route groups?

A.   True

B.   False

35:

Which of the following is a correct way to assign middleware 'auth' to a route?

A.   Route::get('profile', [ 'middleware' => 'auth', 'uses' => 'UserController@show' ]);

B.   Route::get('profile', [ 'controller' => 'auth', 'uses' => 'UserController@show' ]);

C.   Route::get('profile', [ 'secure' => 'auth', 'uses' => 'UserController@show' ]);

D.   Route::get('profile', [ 'filter' => 'auth', 'uses' => 'UserController@show' ]);

36:

Which of the following file contains the database configurations?

A.   config/db.php

B.   public/database.php

C.   config/config.php

D.   config/database.php

37:

If you don't want Eloquent to automatically manage created_at and updated_at columns, which of the following will be correct?

A.   Set the model $timestamps property to false

B.   Eloquent will always automatically manage created_at and updated_at columns

C.   Set the model $created_at and updated_at properties to false

38:

Which of the following methods are provided by migration class?

A.   up()

B.   down()

C.   create()

D.   destroy()

39:

Which of the following command should be used to run all outstanding migration?

A.   php artisan migrate:migration_name

B.   php artisan migrate

C.   php artisan create:migration

D.   php artisan generate:migration

40:

Which method will retrieve the first result of the query in Laravel eloquent?

A.   findOrPass(1);

B.   all(1);

C.   findOrFail(1);

D.   get(1);

41:

The migrations directory contains PHP classes which allow Laravel to update the Schema of your current __, or populate it with values while keeping all versions of the application in sync. Migration files must not be created manually, as their file name contains a timestamp. Instead usetheArtisanCLIinterfacecommand php artisan migrate:make to create a new Migration.

A.   language

B.   config

C.   libraries

D.   database

42:

To retrieve all blog posts that have at least one comment. (Model has one to many relation already set up)

A.   $posts = App\Post::has('comments')->get();

B.   $posts = App\Post::find('comments')->get();

C.   $posts = App\Post::find()->comments()->get();

43:

Eloquent can fire the following events allowing you to hook into various points in the model's lifecycle. (choose all that apply)

A.   creating

B.   created

C.   updating

D.   restoring

44:

All eloquent models protect against mass-assignment by default.

A.   True

B.   False

45:

Creating database view in migration laravel 5.2

A.   CREATE VIEW wones AS SELECT (name from leagues) AS name join teams where (leagues.id = team.country_id) join players where (teams.id = players.team_id) join points where (players.id = points.player_id) sum(points.remnants) AS trem count(points.remnants where points.remnants < 4) AS crem

B.   CREATE VIEW wones AS SELECT leagues.name, sum(points.remnants) AS trem sum(IF(points.remnants<4, 1, 0)) AS crem FROM leagues JOIN teams ON (leagues.id = team.country_id) JOIN players ON (teams.id = players.team_id) JOIN points ON (players.id = points.player_id);

C.   DB::statement( 'CREATE VIEW wones AS SELECT leagues.name as name, sum(points.remnants) as trem, count(case when points.remnants < 4 then 1 end) as crem FROM leauges JOIN teams ON (teams.league_id = leagues.id) JOIN players ON (players.team_id = teams.id) JOIN points ON (points.player_id = players.id); ' );

D.   All of the above

46:

Which of the following is correct to get all rows from the table named users?

A.   DB::list('users')->get();

B.   DB::table('users');

C.   DB::table('users')->all();

D.   DB::table('users')->get();

47:

Which of the following is correct to retrieve soft deleted models?

A.   $flights = App\Flight::withTrashed()->get();

B.   $flights = App\Flight::onlyTrashed()->get();

C.   $flights = App\Flight::onlyDeleted()->get();

D.   $flights = App\Flight::Trashed()->get();

48:

Which of the following databases are supported by Laravel 5?

A.   PostgreSQL

B.   MySQL

C.   SQLite

D.   MongoDB

49:

Get only selected column from database table.

A.   $selected_vote = users_details::lists('selected_vote');

B.   $selected_vote = users_details::all()->get('selected_vote');

C.   $selected_vote = users_details::get(['selected_vote']);

D.   All of the above

50:

Which of the following is correct to retrieve all comments which are associated with single Post (post_id = 1) with one to many relation in Model?

A.   $comments = App\Post::comments->find(1);

B.   $comments = App\Post::find()->comments(1);

C.   $comments = App\Post::find(1)->comments;