laravel basic exmaple: 18 MCQs with answers

All 18 multiple-choice questions from this kit, free and with no sign-up.

Warm-up · 6 questions

Q1. Which file does every incoming Laravel request pass through first?

Beginner
A `app/Http/Controllers` B `public/index.php` C `routes/web.php` D `resources/views`
Show answer & explanation

`public/index.php`

The request enters through `public/index.php`, which bootstraps the framework and hands the request to the router. Routes, controllers, and views are reached later in the pipeline.

Q2. In which file are web routes defined?

Beginner
A `routes/web.php` B `app/Models/Post.php` C `public/index.php` D `resources/views/index.blade.php`
Show answer & explanation

`routes/web.php`

Web routes live in `routes/web.php`, where URLs and HTTP verbs are mapped to closures or controller methods.

Q3. What does the Blade syntax `{{ $name }}` do?

Beginner
A Outputs the value of `$name` B Runs a database query for `$name` C Imports the `$name` controller D Defines a new route named `$name`
Show answer & explanation

Outputs the value of `$name`

Double curly braces are Blade's output syntax. They echo the value of the variable, escaped for safety. Control flow uses directives like `@if` and `@foreach`.

Q4. Which Artisan command applies pending migrations to the database?

Beginner
A `php artisan make:model` B `php artisan make:migration` C `php artisan migrate` D `php artisan serve`
Show answer & explanation

`php artisan migrate`

`php artisan migrate` runs all pending migrations. `make:migration` only creates the migration file; it does not apply it.

Q5. A developer writes a route that returns a plain string with no controller and no view. What is this route best suited for?

Beginner
A A production feature with reuse B A Blade template render C A database migration D A tiny demo or quick test
Show answer & explanation

A tiny demo or quick test

Closure routes that return a string are the absolute minimum working example. They suit tiny demos and quick checks; real features move logic into controllers.

Q6. Laravel follows the MVC pattern, separating routes, controllers, models, and views into distinct layers.

Beginner
A True B False
Show answer & explanation

True

Laravel follows the MVC pattern, separating routes, controllers, models, and views into distinct layers.

Building up · 7 questions

Q7. A controller method needs to send a variable into a Blade view. Which call does this correctly?

Intermediate
A `return route('hello', ['name' => 'Veda']);` B `return model('hello', ['name' => 'Veda']);` C `return view('hello', ['name' => 'Veda']);` D `return migrate('hello', ['name' => 'Veda']);`
Show answer & explanation

`return view('hello', ['name' => 'Veda']);`

The `view()` helper takes the template name and an array of data. The array keys become variables available inside the Blade template.

Q8. Why does Laravel separate routes, controllers, and views into different layers?

Intermediate
A So each layer holds one responsibility and stays readable B Because Eloquent refuses to work with closures C Because Blade cannot run inside a controller D Because Artisan requires exactly three files per feature
Show answer & explanation

So each layer holds one responsibility and stays readable

Routes map URLs to actions, controllers hold logic, and views hold presentation. Keeping each layer to one job is the habit a basic example teaches; mixing them breaks down as the app grows.

Q9. A learner puts a database query directly inside a Blade template. Why is this considered a mistake?

Intermediate
A Migrations must run before any query B Views should hold presentation, not data logic C Blade cannot execute PHP at all D Eloquent models cannot be used in templates
Show answer & explanation

Views should hold presentation, not data logic

Blade compiles to plain PHP, so a query technically runs — but views are for presentation. Data fetching belongs in the controller or model, keeping each layer to one responsibility.

Q10. What does `Post::all()` return in a basic Laravel example?

Intermediate
A A collection of all rows from the `posts` table B The schema of the `posts` table C A single row matching the first record D The migration file for `posts`
Show answer & explanation

A collection of all rows from the `posts` table

`Post::all()` fetches every row from the `posts` table as a collection of `Post` model instances, ready to be passed to a view.

Q11. A team clones a Laravel project and needs the same database structure on a new machine. What makes this reproducible?

Intermediate
A Migrations, applied with `php artisan migrate` B Blade templates, rendered on first request C Eloquent models, created with `make:model` D Closure routes, defined in `web.php`
Show answer & explanation

Migrations, applied with `php artisan migrate`

Migrations are version-controlled PHP files defining table structure. Running `php artisan migrate` recreates the database with one command, avoiding manual SQL and drift between machines.

Q12. Which statement correctly describes the role of `php artisan make:controller`?

Intermediate
A It registers the controller's routes automatically B It scaffolds a controller file with boilerplate to fill in C It creates the database table the controller uses D It compiles the controller into a Blade view
Show answer & explanation

It scaffolds a controller file with boilerplate to fill in

Artisan's `make:` commands generate boilerplate files. They do not register routes, create tables, or compile views — you still write the logic and wire the route yourself.

Q13. A controller action must show only published posts. Which Eloquent call expresses this most clearly?

Intermediate
A `Post::where('published', true)->get()` B `Post::all()->published()` C `Post::migrate('published')` D `Post::view('published')`
Show answer & explanation

`Post::where('published', true)->get()`

Eloquent's query builder chains `where()` with `get()`, reading almost like English while hiding raw SQL. The other calls are not valid Eloquent query methods.

Topper level · 5 questions

Q14. A basic example starts with a closure route and is later refactored into a controller route. What does this progression best illustrate?

Advanced
A Why Blade cannot render controller data B Why Laravel separates concerns as features grow C Why closures are deprecated in Laravel D Why migrations must precede controllers
Show answer & explanation

Why Laravel separates concerns as features grow

Closure routes are fine for tiny demos, but as logic grows, controllers keep routes readable and logic testable and reusable. The refactor itself demonstrates the value of separation of concerns.

Q15. A developer needs a route that is easy to unit test and reused by three different URLs. Which choice fits best?

Advanced
A A Blade view that decides which URL was hit B A controller method referenced by each route C Three separate closure routes with copied logic D One closure route that inspects the URL
Show answer & explanation

A controller method referenced by each route

Controllers group logic into testable methods and let multiple routes point at the same action. Copied closures duplicate logic, and views should never decide routing.

Q16. A learner claims a basic Laravel example is 'just a toy' and shares nothing with a production app. Why is this reasoning flawed?

Advanced
A Production apps avoid Eloquent models B Production apps are the same loop scaled up C Production apps skip routes entirely D Production apps do not use Blade views
Show answer & explanation

Production apps are the same loop scaled up

Every large Laravel app begins as a basic example: one route, one controller, one view, one model. The request-to-response loop, MVC separation, and Artisan workflow stay identical — only the scale changes.

Q17. Given the following Laravel code snippet, identify the correct sequence of operations that occur when a request is made to the '/hello' route: ```php Route::get('/hello', [HelloController::class, 'index']); ``` What happens after the request is received?

Advanced
A The request is routed to the HelloController, which processes the logic and returns a view. B The request is handled by a closure function defined in the routes file. C The request is sent to the Blade view directly, skipping the controller. D The router directly returns a string response without involving any controller.
Show answer & explanation

The request is routed to the HelloController, which processes the logic and returns a view.

The correct option is true because the request to '/hello' is routed to the `HelloController`, which processes the request and returns a view. This illustrates the MVC structure where the controller is responsible for handling the request logic.

Q18. Consider the following migration command and its purpose: ```bash php artisan make:migration create_posts_table ``` What is the significance of running this command in the context of a basic Laravel example, and what subsequent command is necessary to apply the migration?

Advanced
A It defines the structure of the 'posts' table, and you must run 'php artisan migrate' to apply it. B It automatically creates the 'posts' table in the database without additional commands. C It is only necessary for large applications, not for basic examples. D It creates a migration file, but no further action is needed to create the database table.
Show answer & explanation

It defines the structure of the 'posts' table, and you must run 'php artisan migrate' to apply it.

The correct option is true because running the migration command creates a migration file that defines the structure of the 'posts' table. To actually create the table in the database, you must run 'php artisan migrate', which applies the migration.

Make your own

Your notes can be a kit like these

Every kit here was made from real study material. Point Veda at a chapter, a PDF, a photo or a link and get notes, flashcards, practice MCQs, games and a scored test — free, in 42 languages.

  • Chapter notes
  • Flashcards
  • Practice MCQs
  • Scored tests
  • Exam prep

Get Veda free See what students get

Free to start · No ads · Works in 42 languages