0
0
Laravelframework~20 mins

MVC architecture in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 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.