Bird
Raised Fist0
Laravelframework~20 mins

MVC architecture in Laravel - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Laravel MVC Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
What does this Laravel Controller method return?
Consider this Laravel controller method. What will be the output when this method is called in a route?
Laravel
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class ProductController extends Controller {
    public function show($id) {
        $product = ['id' => $id, 'name' => 'Book', 'price' => 15];
        return view('product.detail', ['product' => $product]);
    }
}
AIt returns a view named 'product.detail' with a variable 'product' containing the product array.
BIt returns a JSON response with the product data.
CIt returns a plain text string with the product name.
DIt returns a redirect to the 'product.detail' route.
Attempts:
2 left
💡 Hint
Remember that the view() helper loads a Blade template with data.
❓ state_output
intermediate
1:30remaining
What is the value of $user after this Eloquent query?
Given this Eloquent model query, what will be the value of $user if a user with id 5 exists?
Laravel
<?php
use App\Models\User;
$user = User::find(5);
A$user is a collection of all users including id 5.
B$user is an array containing user data with id 5.
C$user is null because find() returns nothing.
D$user is an instance of User model with id 5 and its data loaded.
Attempts:
2 left
💡 Hint
Eloquent's find() returns a model instance or null.
📝 Syntax
advanced
1:30remaining
Which route definition correctly uses a controller method in Laravel?
Choose the correct syntax to define a GET route that calls the 'index' method of 'PostController'.
ARoute::get('/posts', PostController@index);
BRoute::get('/posts', 'PostController@index');
CRoute::get('/posts', [PostController::class, 'index']);
DRoute::get('/posts', function() { PostController@index(); });
Attempts:
2 left
💡 Hint
Modern Laravel uses array syntax with ::class for controllers.
🔧 Debug
advanced
2:00remaining
Why does this Laravel Blade template cause an error?
This Blade template tries to display a user's name but causes an error. Why? @php $user = null; @endphp

User: {{ $user->name }}

ABecause $user is null, accessing $user->name causes an error.
BBecause Blade does not allow PHP variables inside {{ }}.
CBecause the @php directive cannot define variables.
DBecause the variable $user is not passed from the controller.
Attempts:
2 left
💡 Hint
Think about what happens when you try to access a property on null.
🧠 Conceptual
expert
2:30remaining
In Laravel's MVC, which component is responsible for validating user input before saving to the database?
Where should input validation logic be placed in Laravel's MVC architecture to keep code clean and maintainable?
AIn the View, using JavaScript validation only.
BIn the Controller, using Form Request classes or validation methods.
CDirectly inside the Model's save() method.
DIn the Route definition using middleware only.
Attempts:
2 left
💡 Hint
Think about separation of concerns and Laravel best practices.

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