0
0
Laravelframework~20 mins

Including sub-views (@include) in Laravel - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Blade Include Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
1:30remaining
What will be rendered by this Blade template?
Given the main view includes a sub-view with @include('subview', ['name' => 'Alice']), and the sub-view contains Hello, {{ $name }}!, what is the output when rendering the main view?
Laravel
@include('subview', ['name' => 'Alice'])

// subview.blade.php content:
// Hello, {{ $name }}!
AHello, !
BError: Undefined variable $name
CHello, {{ $name }}!
DHello, Alice!
Attempts:
2 left
💡 Hint
Remember that variables passed in the second argument of @include are available inside the sub-view.
📝 Syntax
intermediate
1:30remaining
Which @include syntax correctly passes multiple variables to a sub-view?
You want to include a sub-view and pass variables $title = 'Welcome' and $count = 5. Which option is correct?
A@include('header', ['title', 'Welcome', 'count', 5])
B@include('header', ['title' => 'Welcome', 'count' => 5])
C@include('header', 'title' => 'Welcome', 'count' => 5)
D@include('header', title: 'Welcome', count: 5)
Attempts:
2 left
💡 Hint
Check the correct syntax for passing an associative array as the second argument.
🔧 Debug
advanced
2:00remaining
Why does this @include cause an error?
This Blade code causes an error:

@include('footer', ['year' => $year])

But the variable $year is defined only in the parent view inside a @section('content'). Why?
Laravel
@section('content')
  @include('footer', ['year' => $year])
  @php $year = 2024; @endphp
@endsection
ABecause $year is defined inside a section and not available outside it when @include runs.
BBecause @include cannot accept variables from the parent view.
CBecause the variable $year must be global to be passed to sub-views.
DBecause @section blocks cannot contain @include directives.
Attempts:
2 left
💡 Hint
Think about variable scope inside Blade sections and when @include is processed.
component_behavior
advanced
1:30remaining
What happens if the included sub-view does not exist?
Consider this Blade code:

@include('nonexistent')

What will Laravel do when rendering this view?
Laravel
@include('nonexistent')
ALaravel throws an error: View [nonexistent] not found.
BLaravel renders the parent view but outputs a warning inside the HTML.
CLaravel renders nothing and continues without error.
DLaravel creates an empty file named 'nonexistent' automatically.
Attempts:
2 left
💡 Hint
Think about how Laravel handles missing views during rendering.
state_output
expert
2:30remaining
What is the output of this nested @include with variable shadowing?
Given these Blade files:

// main.blade.php @php $color = 'red'; @endphp @include('subview', ['color' => 'blue']) // subview.blade.php Color: {{ $color }}
@include('subsubview') // subsubview.blade.php Color again: {{ $color }}


What is the full rendered output of main.blade.php?
Laravel
// main.blade.php
@php $color = 'red'; @endphp
@include('subview', ['color' => 'blue'])

// subview.blade.php
Color: {{ $color }}<br>
@include('subsubview')

// subsubview.blade.php
Color again: {{ $color }}
AColor: red<br>Color again: red
BColor: blue<br>Color again: red
CColor: blue<br>Color again: blue
DColor: red<br>Color again: blue
Attempts:
2 left
💡 Hint
Consider how variables passed to @include affect nested includes and variable scope.