Create New Post

Laravel MCQs - 10

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

 

A) Option A: Defines a route for displaying a specific post.

B) Option B: Defines a route for creating a new post.

C) Option C: Defines a route for updating a post.

D) Option D: Defines a route for deleting a post.

Answer: A) Option A

  1. Which of the following code snippets demonstrates how to define a named route with parameters in Laravel?
 Route::get('/products/{category}/{id}', 'ProductController@show')->name('product.show');

A) Option A: Defines a named route for viewing a product's details.

B) Option B: Defines a named route for adding a product to the cart.

C) Option C: Defines a named route for editing a product.

D) Option D: Defines a named route for deleting a product.

Answer: A) Option A

  1. Which of the following code snippets demonstrates how to define a controller method to handle form submission in Laravel?
 public function store(Request $request)
{
    $data = $request->validate([
        'title' => 'required|string|max:255',
        'content' => 'required|string',
    ]);

    Post::create($data);

    return redirect()->route('posts.index')->with('success', 'Post created successfully.');
}

A) Option A: Defines a method for updating a post.

B) Option B: Defines a method for deleting a post.

C) Option C: Defines a method for displaying a list of posts.

D) Option D: Defines a method for creating a new post.

Answer: D) Option D

  1. Which of the following code snippets demonstrates how to define a database migration to add a column to an existing table in Laravel?
 use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class AddStatusColumnToPostsTable extends Migration
{
    public function up()
    {
        Schema::table('posts', function (Blueprint $table) {
            $table->boolean('status')->default(true);
        });
    }

    public function down()
    {
        Schema::table('posts', function (Blueprint $table) {
            $table->dropColumn('status');
        });
    }
}

A) Option A: Adds a "status" column to the "posts" table.

B) Option B: Adds a "category" column to the "posts" table.

C) Option C: Adds a "tag" column to the "posts" table.

D) Option D: Adds a "user_id" column to the "posts" table.

Answer: A) Option A

  1. Which of the following code snippets demonstrates how to define a one-to-many relationship in an Eloquent model with custom foreign key and local key in Laravel?
 class User extends Model
{
    public function posts()
    {
        return $this->hasMany(Post::class, 'author_id', 'id');
    }
}

A) Option A: Defines a one-to-many relationship between users and posts with the default foreign and local keys.

B) Option B: Defines a one-to-many relationship between users and posts with a custom foreign key but the default local key.

C) Option C: Defines a one-to-many relationship between users and posts with the default foreign key but a custom local key.

D) Option D: Defines a one-to-many relationship between users and posts with custom foreign and local keys.

Answer: D) Option D

Comments

Leave a Reply

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

67585