Including Sub-Views with @include in Laravel Blade
📖 Scenario: You are building a simple Laravel web page that shows a main content area and a footer. The footer is a separate part that you want to reuse on multiple pages.
🎯 Goal: Create a main Blade view that includes a footer sub-view using the @include directive.
📋 What You'll Learn
Create a Blade view file named footer.blade.php with a footer message.
Create a main Blade view file named main.blade.php with a heading and include the footer sub-view using @include.
Use the exact footer message: "This is the footer content."
Use the exact heading text: "Welcome to the Main Page"
💡 Why This Matters
🌍 Real World
Web developers often break pages into smaller parts like headers, footers, and sidebars to reuse them across many pages. This keeps code clean and easier to maintain.
💼 Career
Knowing how to include sub-views is essential for Laravel developers to build modular and maintainable web applications.
Progress0 / 4 steps
1
Create the footer sub-view
Create a Blade view file named footer.blade.php that contains a <footer> element with the text This is the footer content.
Laravel
Hint
Use a <footer> tag and write the exact text inside it.
2
Create the main view with heading
Create a Blade view file named main.blade.php that contains an <h1> element with the text Welcome to the Main Page
Laravel
Hint
Use an <h1> tag with the exact heading text.
3
Include the footer sub-view in the main view
In the main.blade.php file, add the Blade directive @include('footer') below the <h1> element to include the footer sub-view.
Laravel
Hint
Use @include('footer') exactly to include the footer view.
4
Finalize the main view structure
Wrap the content in main.blade.php inside a <main> element and ensure the @include('footer') is inside this <main> block.
Laravel
Hint
Use a <main> tag to wrap the heading and the include directive.
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
Step 1: Understand the purpose of @include
The @include directive is used in Blade templates to insert one view inside another, allowing code reuse.
Step 2: Identify what @include does not do
It does not define routes, create migrations, or compile CSS files, which are unrelated tasks.
Final Answer:
It inserts a sub-view inside the current view to reuse code. -> Option C
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
Step 1: Recall Blade include syntax
The correct syntax uses parentheses and quotes: @include('viewname').
Step 2: Check each option format
Only @include('header') uses the correct parentheses and quotes. Others use invalid symbols.
Final Answer:
@include('header') -> Option A
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
Step 1: Understand @include output
The @include('nav') inserts the entire content of nav.blade.php into the main view.
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.
Final Answer:
<nav>Menu</nav> -> Option A
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
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.
Step 2: Identify the error type
Laravel reports a runtime error stating the view is not found, not a syntax or warning error.
Final Answer:
Runtime error: View [footer] not found. -> Option D
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
Step 1: Recall passing data syntax in @include
To pass data, use an associative array as the second argument: @include('view', ['key' => value]).
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.
Final Answer:
@include('header', ['title' => 'Welcome']) -> Option B
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 unknownstrongwhile earning +1 Grip toward your certificate.