Bird
Raised Fist0
Laravelframework~10 mins

MVC architecture in Laravel - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define a route that uses the HomeController's index method.

Laravel
Route::get('/', [[1]::class, 'index']);
Drag options to blanks, or click blank then click option'
AController
Bweb
CRoute
DHomeController
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'web' instead of the controller name.
Using 'Route' or 'Controller' which are not controller class names.
2fill in blank
medium

Complete the code to create a new controller named ProductController using Artisan command.

Laravel
php artisan make:[1] ProductController
Drag options to blanks, or click blank then click option'
Amigration
Bmodel
Ccontroller
Dseeder
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'model' or 'migration' which create different classes.
Using 'seeder' which is for database seeding.
3fill in blank
hard

Fix the error in the controller method to return a view named 'welcome'.

Laravel
public function index() {
    return [1]('welcome');
}
Drag options to blanks, or click blank then click option'
Aview()
BView()
Creturn()
Drender()
Attempts:
3 left
💡 Hint
Common Mistakes
Using uppercase 'View()' which causes an error.
Using 'return()' or 'render()' which are not correct here.
4fill in blank
hard

Fill both blanks to pass data from controller to view with variable 'name' set to 'Laravel'.

Laravel
return view('greeting', [[1] => [2]]);
Drag options to blanks, or click blank then click option'
A'name'
B'Laravel'
C'view'
D'data'
Attempts:
3 left
💡 Hint
Common Mistakes
Using variable names without quotes.
Swapping key and value positions.
5fill in blank
hard

Fill all three blanks to define a model named Post with a fillable property for 'title' and 'content'.

Laravel
class [1] extends Model {
    protected $[2] = [[3]];
}
Drag options to blanks, or click blank then click option'
APost
Bfillable
C'title', 'content'
Dtimestamps
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'timestamps' instead of 'fillable'.
Not quoting the array elements properly.

Practice

(1/5)
1. In Laravel's MVC architecture, which component is responsible for handling user input and application logic?
easy
A. Controller
B. Model
C. View
D. Route

Solution

  1. Step 1: Understand MVC roles

    Model handles data, View handles UI, Controller handles logic and user input.
  2. Step 2: Identify the component for logic

    Controller processes requests, applies logic, and returns responses.
  3. Final Answer:

    Controller -> Option A
  4. Quick Check:

    Logic and input handling = Controller [OK]
Hint: Controller = logic and input handler in MVC [OK]
Common Mistakes:
  • Confusing Model with Controller
  • Thinking View handles logic
  • Assuming Route handles logic
2. Which of the following is the correct way to define a route that calls the show method of UserController in Laravel?
easy
A. Route::get('/user/{id}', [UserController, 'show']);
B. Route::get('/user/{id}', 'show@UserController');
C. Route::get('/user/{id}', function() { UserController@show(); });
D. Route::get('/user/{id}', 'UserController@show');

Solution

  1. Step 1: Recall Laravel route syntax

    Routes use Route::get('uri', 'Controller@method') format for controller methods.
  2. Step 2: Match correct syntax

    Route::get('/user/{id}', 'UserController@show'); matches this pattern exactly; others have syntax errors or wrong order.
  3. Final Answer:

    Route::get('/user/{id}', 'UserController@show'); -> Option D
  4. Quick Check:

    Correct route syntax = Route::get('/user/{id}', 'UserController@show'); [OK]
Hint: Use 'Controller@method' string in routes [OK]
Common Mistakes:
  • Using array without quotes
  • Swapping controller and method order
  • Using anonymous function incorrectly
3. Given this Laravel controller method, what will be the output when accessing /welcome route?
public function welcome() {
    $name = 'Alice';
    return view('greeting', ['name' => $name]);
}

Assuming the greeting.blade.php view contains:
<h1>Hello, {{ $name }}!</h1>
medium
A. Error: Undefined variable $name
B.

Hello, Alice!

C.

Hello, $name!

D.

Hello, !

Solution

  1. Step 1: Understand data passing to view

    The controller passes ['name' => 'Alice'] to the view as variable $name.
  2. Step 2: Interpret Blade template output

    The view uses {{ $name }} which outputs 'Alice' safely inside <h1> tags.
  3. Final Answer:

    <h1>Hello, Alice!</h1> -> Option B
  4. Quick Check:

    Passed variable rendered correctly = <h1>Hello, Alice!</h1> [OK]
Hint: Variables passed to view appear as {{ $variable }} [OK]
Common Mistakes:
  • Expecting raw variable name output
  • Confusing PHP variable syntax inside Blade
  • Assuming missing variable error
4. Identify the error in this Laravel controller method:
public function index() {
    $users = User::all;
    return view('users.index', compact('users'));
}
medium
A. Missing parentheses after all() method
B. Incorrect view name syntax
C. compact() should be replaced with array()
D. User model not imported

Solution

  1. Step 1: Check method call syntax

    User::all is a method and requires parentheses: User::all() to fetch all users.
  2. Step 2: Verify other parts

    View name and compact usage are correct; import is assumed correct or handled by namespace.
  3. Final Answer:

    Missing parentheses after all() method -> Option A
  4. Quick Check:

    Method calls need () = Missing parentheses after all() method [OK]
Hint: Remember to add () when calling methods [OK]
Common Mistakes:
  • Forgetting parentheses on method calls
  • Misusing compact() function
  • Assuming view name syntax error
5. You want to display a list of products with their categories in a Laravel app using MVC. Which approach correctly follows MVC principles?
hard
A. Use Controller only to render HTML, fetching done in View
B. Write SQL queries directly in the View to get products and categories
C. Fetch products and categories in Controller, pass to View, and display there
D. Put all fetching and HTML rendering inside the Model

Solution

  1. Step 1: Recall MVC responsibilities

    Model handles data, Controller handles logic and data fetching, View handles display.
  2. Step 2: Evaluate options

    Fetch products and categories in Controller, pass to View, and display there correctly fetches data in Controller and passes it to View for display, following MVC.
  3. Final Answer:

    Fetch products and categories in Controller, pass to View, and display there -> Option C
  4. Quick Check:

    Controller fetches, View displays = Fetch products and categories in Controller, pass to View, and display there [OK]
Hint: Controller fetches data; View only displays it [OK]
Common Mistakes:
  • Putting SQL in Views
  • Mixing data logic in Models with display
  • Letting Views fetch data
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard