Laravel Framework Test Upwork top 20%

/
/
2274 Views

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

{{ secure }}

{{ csrf_field() }}

{{ protect() }}

{{ csrf_protect() }}

Ans: (B) {{csrf_field()}}

2. Suppose we have a collection which contains the website news. What is the best way to share that collection in all views?

$news=NewsStory::orderBy(‘created_at’, ‘desc’)->paginate(5);

view()->share(‘news’, NewsStory::orderBy(‘created_at’, ‘desc’)->paginate(5));

view()->addShare(‘news’, NewsStory::orderBy(‘created_at’, ‘desc’)->paginate(5));

All of the above

Ans: (B) view()->share(‘news’, NewsStory::orderBy(‘created_at’, ‘desc’)->paginate(5));

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

Route::any()

Route::match()

Route::all()

Ans: (A) Route::any()

4. 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 the correct validation rule?

‘finish_date’ => ‘required|date|after|start_date’

‘finish_date’ => ‘required|date|>:start_date’

‘finish_date’ => ‘required|date|after:start_date’

‘finish_date’ => ‘required|date|greater:start_date’

Ans(C: ) ‘finish_date’ => ‘required|date|after:start_date’

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

DB::list(‘users’)->get();

DB::table(‘users’);

DB::table(‘users’)->all();

DB::table(‘users’)->get();

Ans(D: ) DB::table(‘users’)->get();

6. If we have a following URL

http://myapp.dev/?foo=bar&baz=boo

Which of the following will get the values of ‘foo’ and ‘baz’?

Request::get(‘foo’, ‘baz’);

Request::only([‘foo’, ‘baz’]);

Request::except(‘foo’, ‘baz’);

Ans(B: ) Request::only([‘foo’, ‘baz’]);

7. How do you add default value to a select list in form.

Form::getselect(

‘myselect’,

$categories,

$myselectedcategories,

array(

‘class’ => ‘form-control’,

‘id’ => ‘myselect’

)

}}

Form::getselect(

‘myselect’,

array_merge([” => [‘label’ => ‘Please Select’, ‘disabled’ => true], $categories),

$myselectedcategories,

array(

‘class’ => ‘form-control’,

‘id’ => ‘myselect’

)

}}

Form::select(

‘myselect’,

array_merge([” => [‘label’ => ‘Please Select’, ‘disabled’ => true], $categories),

$myselectedcategories,

array(

‘class’ => ‘form-control’,

‘id’ => ‘myselect’

)

}}

Form::select(

‘myselect’,

$categories,

$myselectedcategories,

array(

‘class’ => ‘form-control’,

‘id’ => ‘myselect’

)

}}

Ans (c: )

8. Which of the following is correct Blade for/else syntax?

<?php if (empty($users)): ?>

No users.

<?php else: ?>

<?php foreach ($users as $user): ?>

<?= $user->first_name ?> <?= $user->last_name ?><br>

<?php endforeach; ?>

<?php endif; ?>

{% for user in users %}

{{ user.first_name }} {{ user.last_name }}<br>

{% else %}

No users.

{% endfor %}

@forelse ($users as $user)

{{ $user->first_name }} {{ $user->last_name }}<br>

@empty

No users.

@endforelse

@foreach ($users as $user)

{{ $user->first_name }} {{ $user->last_name }}<br>

@else

No users.

@endforeach

9. Which of the following are correct route methods?

Note: There may be more than one right answer.

Route::get($uri, $callback);

Route::options($uri, $callback);

Route::put($uri, $callback);

Route::delete($uri, $callback);

Ans(ALL )

10. Which of the following validation rules are acceptable?

Note: There may be more than one right answer.

[‘field’ => ‘accepted’]

[‘field’ => ‘active_url’]

[‘field’ => ‘after:10/10/16’]

[‘field’ => ‘alpha’]

Ans(A:B:D )

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

Cache::permanent(‘key’, ‘value’);

Cache::add(‘key’, ‘value’);

Cache::put(‘key’, ‘value’);

Cache::forever(‘key’, ‘value’);

Ans(D: )

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

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

You may place a single __construct method on the controller

You may place a single __invoke method on the controller

You may place a single __create method on the controller

You can not create single action controller in laravel

Ans ( B:)

13. Which one of the following is correct form encoding for file uploads?

<form method=”post” type=”multipart/form-data”>

<form method=”post”>

<form method=”get” enctype=”multipart/form-data”>

<form method=”post” enctype=”multipart/form-data”>

Ans(D: )

14. Which of the following can be used in views to print a message if array is empty?

<ul>

@while ($names as $name)

<li>{{ $name }}</li>

@else

<li>Don’t have names to list.</li>

@endwhile

</ul>

<ul>

@if ($names as $name)

<li>{{ $name }}</li>

@else

<li>Don’t have names to list.</li>

@endif

</ul>

<ul>

@for ($names as $name)

<li>{{ $name }}</li>

@else

<li>Don’t have names to list.</li>

@endfor

</ul>

<ul>

@forelse ($names as $name)

<li>{{ $name }}</li>

@empty

<li>Don’t have names to list.</li>

@endforelse

</ul>

Ans(D: )

15. Which of the following is correct to get the URL of named route ‘contact’?

$url = route(‘contact’);

$url = route()->name(‘contact’);

$url = name(‘contact’);

$url = route::name(‘contact’);

Ans(A: )

16. Which of the following databases are supported by Laravel 5?

Note: There may be more than one right answer.

PostgreSQL

MySQL

SQLite

MongoDB

Ans(ALL: )

17. Which of the following are valid validation rules? (Choose all that apply)

Note: There may be more than one right answer.

‘name’ => ‘required|max:255′

’email’ => ’email’

‘website’ => [‘regex:/^((?:https?\:\/\/|www\.)(?:[-a-z0-9]+\.)*[-a-z0-9]+.*)$/’]

‘re_occurance_end_date’ => ‘required_if:repeat_appointment,daily,weekly,monthly,yearly’

18. Which of the following is correct command to save an uploaded file?

$request->file(‘avatar’)->store(‘avatars’);

$request->input(‘avatar’)->store(‘avatars’);

$request->file(‘avatar’)->save(‘avatars’);

$request->input(‘avatar’)->save(‘avatars’);

Ans(A: )

19. Which of the following loops are available in blade? (choose all that apply)

Note: There may be more than one right answer.

@for ($i = 0; $i < 10; $i++)

The current value is {{ $i }}

@endfor

@foreach ($users as $user)

<p>This is user {{ $user->id }}</p>

@endforeach

@forelse ($users as $user)

<li>{{ $user->name }}</li>

@empty

<p>No users</p>

@endforelse

@while (true)

<p>I’m looping forever.</p>

@endwhile

Ans(all)

21. Which of the following is correct to redirect named route ‘photos.index’?

No answer is correct according to doc ….

Route::get(‘redirect’, function () {

return Redirect::to(‘photos.index’);

});

Route::get(‘redirect’, function () {

return Redirect::route(‘photos.index’);

});

Route::get(‘redirect’, function () {

return Redirect(‘photos.index’);

});

Route::get(‘redirect’, function () {

return Redirect::route(‘photos’);

});

22. 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

True

False

Ans: TRUE

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

$iterate

$first

$index

$loop

Ans(D: )

25. ____ 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.

Routing

Module

Views

Forms

Ans (A: )

26. Which of the following is correct to loop over an array named $lists in view?

<ul>

@foreach ($lists as $list)

<li>{{ $list }}</li>

@endforeach

</ul>

<ul>

{ foreach ($lists as $list) }

<li>{{ $list }}</li>

{ endforeach }

</ul>

<ul>

@foreach ($list as $lists)

<li>{{ $list }}</li>

@endforeach

</ul>

<ul>

@foreach ($lists as $list)

<li>{{ $list }}</li>

@end

</ul>

Ans(A: )

27. Which of the following are correct regular expression route constraints?

Note: There may be more than one right answer.

Route::get(‘users/{id}’, function ($id) {

//

})->where(‘id’, ‘[0-9]+’);

Route::get(‘users/{username}’, function ($username) {

//

})->where(‘username’, ‘[A-Za-z]+’);

Route::get(‘posts/{id}/{slug}’, function ($id, $slug) {

//

})->where([‘id’ => ‘[0-9]+’, ‘slug’ => ‘[A-Za-z]+’]);

Route::get(‘users/id’, function ($id) {

//

})->where(‘id’, ‘[1-9]+’);

Ans (All)

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

Note: There may be more than one right answer.

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

Specify your custom messages in a language file.

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

Ans: (All)

29. Which command should be used to run all outstanding migration?

php artisan migrate:migration_name

php artisan migrate

php artisan create:migration

php artisan generate:migration

Ans: (B: )

30. Which of the following validator methods returns ‘True’ when form data is valid?

fails()

passes()

valid()

success()

Ans (B: )

31. Which of the following are correct route definitions? (Choose all that apply)

Note: There may be more than one right answer.

Route::match([‘get’, ‘post’], ‘/’, function () {});

Route::any(‘/’, function () {});

Route::delete(‘/’, function () {});

Route::put(‘/’, function () {});

Ans(All)

32. To use

@section(‘content’)

Welcome to your application dashboard!

@endsection

in your blade view what else should be there at the top?

@yeild(‘layouts.master’)

@extends(‘layouts.master’)

@include(‘layouts.master’)

@require(‘layouts.master’)

33. Which of the following routes are created by below route declaration? (Choose all that apply)

Route::resource(‘photos’, ‘PhotoController’);

Note: There may be more than one right answer.

/photos

/photos/create

/photos/{photo}/edit

/photos/{photo}/delete

34.Which of the following is convenient and correct way to automatically inject the model instances directly into your routes?

Route::get(‘api/users/{user}’, function ($user) {

return $user->email;

});

Route::get(‘users/{user}’, function (App\User $user) {

return $user->email;

});

Route::get(‘users/{id}’, function (App\User $user) {

return $user->email;

});

Ans (B)

35. Which of the following methods can be used to retrieve the record as an object containing the column names and the data stored in the record?

get()

find()

add()

insert()

Ans(B : )

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

Set the model $timestamps property to false

Eloquent will always automatically manage created_at and updated_at columns

Set the model $created_at and updated_at properties to false

Ans (A: )

37. Which of the following is correct directory for view files?

app/views

storage/views

resources/views

public/views

Ans (C: )

38. To define a callback when view is rendered we can use:

View::composer()

View::callback()

View::method()

View::match()

Ans : (A: )

39. Which of the following is correct to redirect named route ‘photos.index’?

Same question above

Route::get(‘redirect’, function () {

return Redirect::to(‘photos.index’);

});

Route::get(‘redirect’, function () {

return Redirect::route(‘photos.index’);

});

Route::get(‘redirect’, function () {

return Redirect(‘photos.index’);

});

Route::get(‘redirect’, function () {

return Redirect::route(‘photos’);

});

40. Which of the following artisan command is correct to create a model with table named ‘projects’?

php artisan make:model Project

php artisan create:model Project -m

php artisan create:table Project

php artisan make:model Project -m

41. Which following validation rule is correct to validate that the file is an image file having dimensions of min 200 height x min 400 width?

‘avatar’ => ‘dimensions:min_width=100,min_height=200’

‘avatar’ => ‘file:image|dimensions:min_width=100,min_height=200’

‘avatar’ => ‘file:type=image,min_width=100,min_height=200’

‘avatar’ => ‘image:min_width=100,min_height=200’

Ans(All );

42. Which of the following can be applied to route group? (Choose all that apply)

Note: There may be more than one right answer.

A. middleware

b. prefix

c. domain

d. namespace

Ans:All

43. Which of the following are valid blade directives? (Choose all that apply)

Note: There may be more than one right answer.

A. @if, @else, @elseif, and @endif

b. @unless and @endunless

c. @for, @foreach, and @while

d. @forelse, @empty and @endforelse

Ans:All

44. The following option will allow you to upload an image with many to many relationships:

A. $file = Request::file(‘resume’);

$extension = $file->getClientOriginalExtension();

Storage::disk(‘local’)->put($file->getFilename().’.’.$extension, File::get($file));

$resume = new Resume();

$resume->mime = $file->getClientMimeType();

$resume->filename = $file->getFilename().’.’.$extension;

$candidate=new Candidate();

$data=array_except($data, array(‘_token’,’resume’));

$user->candidate()->save($candidate);

$candidate->resume()->save($resume);

$candidate=$user->candidate($user);

$candidate->update($data);

b. public function create()

{

$categories = Category::lists(‘name’, ‘id’);

$days = Day::lists(‘dayname’, ‘id’);

return view(‘articles.create’, compact(‘categories’, ‘days’));

}

public function store(ArticleRequest $request)

{

$image_name = $request->file(‘image’)->getClientOriginalName();

$request->file(‘image’)->move(base_path().’/public/images’, $image_name);

$article = ($request->except([‘image’]));

$article[‘image’] = $image_name;

Article::create($article);

}

c. $file = Request::addfile(‘resume’);

$extension = $files->getClientOriginalExtension();

Storage::disk(‘local’)->put($file->getFilename().’.’.$extension, File::get($file));

$resume = new Resume();

$resume->mime = $file->getClientMimeType();

$resume->filename = $file->getFilename().’.’.$extension;

$candidate=new Candidate();

$data=array_except($data, array(‘_token’,’resume’));

$user->candidate()->save($candidate);

$candidate->resume()->save($resume);

$candidate=$user->candidate($user);

$candidate->update($data);

d.all of the above..

Ans:A

 

 

 

Sign up for our newsletter to get Daily Updates

It is main inner container footer text