Create New Post

Laravel MCQs - 9

  1. Which of the following code snippets demonstrates how to define a middleware in Laravel?
class VerifyAge
{
    public function handle($request, Closure $next)
    {
        if ($request->age >= 18) {
            return $next($request);
        }
        abort(403, 'You must be 18 years or older.');
    }
}

A) class EnsureAdult

B) class AuthenticateUser

C) class CheckPermission

D) class AgeCheck

Answer: A) class EnsureAdult

  1. Which of the following code snippets demonstrates how to define a database seeder in Laravel?
 class DatabaseSeeder extends Seeder
{
    public function run()
    {
        User::factory()->count(10)->create();
    }
}

A) class PopulateUsersTable

B) class SeedData

C) class GenerateData

D) class DatabasePopulator

Answer: A) class PopulateUsersTable

  1. Which of the following code snippets demonstrates how to define a route with a parameter in Laravel?
 Route::get('/users/{id}', function ($id) {
    return User::findOrFail($id);
});

A) Route::get('/user/{id}', function ($id) {

B) Route::get('/user/{userId}', function ($userId) {

C) Route::get('/profile/{id}', function ($id) {

D) Route::get('/users/{userId}', function ($userId) {

Answer: D) Route::get('/users/{userId}', function ($userId) {

  1. Which of the following code snippets demonstrates how to define a one-to-many relationship in an Eloquent model in Laravel?
 class Post extends Model
{
    public function comments()
    {
        return $this->hasMany(Comment::class);
    }
}

A) public function posts()

B) public function commentsList()

C) public function postComments()

D) public function relatedComments()

Answer: C) public function postComments()

  1. Which of the following code snippets demonstrates how to define a form request validation in Laravel?
 class StoreBlogPostRequest extends FormRequest
{
    public function authorize()
    {
        return true;
    }

    public function rules()
    {
        return [
            'title' => 'required|unique:posts|max:255',
            'body' => 'required',
        ];
    }
}

A) class BlogPostValidationRequest

B) class ValidateBlogPost

C) class BlogPostRequest

D) class StoreBlogPostRequest

Answer: D) class StoreBlogPostRequest

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

97658