In this lab, you will learn how to create and use Laravel Blade layouts to structure your application efficiently. The key tasks include:
- Creating a Blade layout template
- Using Blade sections and components
- Extending the layout in child views
- Rendering dynamic content in views
- Create a
layoutsfolder insideresources/views/. - Inside the folder, create a
main.blade.phpfile. - Define the basic structure including
@yieldand@sectiondirectives.
- Create a new Blade file (e.g.,
home.blade.php). - Extend
main.blade.phpusing@extendsand@section. - Add sample content for the homepage.
- Create a Blade partial for reusable elements (e.g., header, footer).
- Use
@includeto include these partials inside the layout. - Create a Blade component for buttons or cards.
- Modify a controller to fetch sample data from the database.
- Pass the data to the Blade view using
compact()orwith(). - Display the data dynamically inside the child view using
{{ }}syntax.
- Create a new route for the homepage in
web.php. - Return the
home.blade.phpview from a controller or directly in the route. - Test the page in the browser.