Bird
Raised Fist0
Laravelframework~20 mins

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

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

Practice

(1/5)
1. What does the @include directive do in a Laravel Blade template?
easy
A. It creates a new database migration file.
B. It defines a new route in the web.php file.
C. It inserts a sub-view inside the current view to reuse code.
D. It compiles all CSS files into one.

Solution

  1. Step 1: Understand the purpose of @include

    The @include directive is used in Blade templates to insert one view inside another, allowing code reuse.
  2. Step 2: Identify what @include does not do

    It does not define routes, create migrations, or compile CSS files, which are unrelated tasks.
  3. Final Answer:

    It inserts a sub-view inside the current view to reuse code. -> Option C
  4. Quick Check:

    @include = Insert sub-view [OK]
Hint: Remember: @include inserts reusable view parts [OK]
Common Mistakes:
  • Confusing @include with routing or migrations
  • Thinking @include compiles assets
  • Assuming @include creates new files
2. Which of the following is the correct syntax to include a Blade sub-view named header?
easy
A. @include('header')
B. @include:header
C. @include[header]
D. @include{header}

Solution

  1. Step 1: Recall Blade include syntax

    The correct syntax uses parentheses and quotes: @include('viewname').
  2. Step 2: Check each option format

    Only @include('header') uses the correct parentheses and quotes. Others use invalid symbols.
  3. Final Answer:

    @include('header') -> Option A
  4. Quick Check:

    Correct syntax uses parentheses and quotes [OK]
Hint: Use parentheses and quotes with @include [OK]
Common Mistakes:
  • Using colons, brackets, or braces instead of parentheses
  • Omitting quotes around view name
  • Writing @include without parentheses
3. Given the following Blade code:
@include('nav')

and the nav.blade.php contains:
<nav>Menu</nav>

What will be the rendered output inside the main view?
medium
A. <nav>Menu</nav>
B. Menu
C. @include('nav')
D. Error: View not found

Solution

  1. Step 1: Understand @include output

    The @include('nav') inserts the entire content of nav.blade.php into the main view.
  2. Step 2: Check the content of nav.blade.php

    It contains the HTML <nav>Menu</nav>, so this exact HTML will appear in the output.
  3. Final Answer:

    <nav>Menu</nav> -> Option A
  4. Quick Check:

    @include inserts full sub-view content [OK]
Hint: Included view content appears exactly where @include is placed [OK]
Common Mistakes:
  • Expecting only inner text without tags
  • Thinking @include outputs the directive text
  • Assuming an error if view exists
4. What is the error in this Blade code snippet?
@include('footer')

Assuming the file footer.blade.php does not exist in the views folder.
medium
A. Warning: Duplicate view inclusion.
B. No error, it renders nothing.
C. Syntax error due to missing semicolon.
D. Runtime error: View [footer] not found.

Solution

  1. Step 1: Check if included view file exists

    The @include('footer') tries to load footer.blade.php. If it does not exist, Laravel throws an error.
  2. Step 2: Identify the error type

    Laravel reports a runtime error stating the view is not found, not a syntax or warning error.
  3. Final Answer:

    Runtime error: View [footer] not found. -> Option D
  4. Quick Check:

    Missing view file causes runtime error [OK]
Hint: Ensure included view files exist to avoid runtime errors [OK]
Common Mistakes:
  • Assuming no error if file missing
  • Confusing syntax errors with missing files
  • Expecting warnings instead of errors
5. How can you pass a variable named title with value 'Welcome' to a sub-view header using @include?
hard
A. @include('header')->with('title', 'Welcome')
B. @include('header', ['title' => 'Welcome'])
C. @include('header', title = 'Welcome')
D. @include('header', {title: 'Welcome'})

Solution

  1. Step 1: Recall passing data syntax in @include

    To pass data, use an associative array as the second argument: @include('view', ['key' => value]).
  2. Step 2: Check each option for correct syntax

    @include('header', ['title' => 'Welcome']) uses the correct array syntax. Options A, C, and D use invalid or unsupported syntax.
  3. Final Answer:

    @include('header', ['title' => 'Welcome']) -> Option B
  4. Quick Check:

    Pass data as array in second argument [OK]
Hint: Pass variables as array in second argument of @include [OK]
Common Mistakes:
  • Using method chaining with @include
  • Passing variables without array brackets
  • Using object or JS-like syntax
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