Laravel / Laravel Views and Blade Templates

Passing Data and Sharing Variables

This tutorial covers how to pass data from a controller to a view and how to share variables across multiple views. We will learn how to use Blade syntax to display this data in o…

Tutorial 2 of 5 5 resources in this section

Section overview

5 resources

Covers building dynamic views using Blade templating engine.

1. Introduction

In this tutorial, we will learn how to pass data from a controller to a view in the Laravel framework and how to share variables across multiple views.

You'll learn:

  • How to pass data from a controller to a view
  • How to share variables across multiple views
  • How to use Blade syntax to display this data in your views

Before we begin, make sure you have a basic understanding of MVC (Model-View-Controller) architecture, PHP, and the Laravel framework.

2. Step-by-Step Guide

Passing Data From Controller to View

In Laravel, you can pass data from a controller to a view using the with function. This function accepts two arguments: the name of the variable you want to use in your view and the value of this variable.

public function show()
{
    return view('welcome')->with('name', 'John Doe');
}

In this example, 'name' is the variable we are passing to the view, and 'John Doe' is its value. You can access this variable in your view using Blade syntax:

Hello, {{ $name }}

Sharing Variables Across Views

You can share variables across multiple views using the share method in the AppServiceProvider:

public function boot()
{
    View::share('key', 'value');
}

In this case, 'key' is the name of the variable, and 'value' is its value. You can access this shared variable in any view:

The shared value is: {{ $key }}

3. Code Examples

Here are some practical examples of how to pass data from controllers to views and share variables across views.

Example 1: Passing Data to a View

Controller:

public function show()
{
    $name = 'John Doe';
    $age = 30;
    return view('welcome', compact('name', 'age'));
}

In this example, we are passing two variables to the view: 'name' and 'age'. We use the compact function to pass multiple variables.

View:

<p>Hello, {{ $name }}. You are {{ $age }} years old.</p>

Example 2: Sharing Variables Across Views

AppServiceProvider:

public function boot()
{
    View::share('siteName', 'My Awesome Site');
}

In any view, you can access the shared variable:

<h1>Welcome to {{ $siteName }}</h1>

4. Summary

In this tutorial, we've learned how to pass data from controllers to views and how to share variables across multiple views in Laravel. We've also learned how to use Blade syntax to display these data in our views.

For further learning, you can explore other features of the Laravel framework. Here are some additional resources:

  • Laravel Documentation
  • Laravel News
  • Laracasts

5. Practice Exercises

  1. Create a controller and pass a list of items to a view. Display these items in the view using Blade syntax.
  2. Share a variable across multiple views. Try to access this shared variable in different views.

Solutions

  1. Controller:
public function show()
{
    $items = ['Apple', 'Banana', 'Cherry'];
    return view('items', compact('items'));
}

View:

<ul>
@foreach ($items as $item)
    <li>{{ $item }}</li>
@endforeach
</ul>
  1. AppServiceProvider:
public function boot()
{
    View::share('siteName', 'My Awesome Site');
}

In any view, you can access the shared variable:

<h1>Welcome to {{ $siteName }}</h1>

Remember to continue practicing and exploring the Laravel documentation to gain more knowledge and experience.

Need Help Implementing This?

We build custom systems, plugins, and scalable infrastructure.

Discuss Your Project

Related topics

Keep learning with adjacent tracks.

View category

HTML

Learn the fundamental building blocks of the web using HTML.

Explore

CSS

Master CSS to style and format web pages effectively.

Explore

JavaScript

Learn JavaScript to add interactivity and dynamic behavior to web pages.

Explore

Python

Explore Python for web development, data analysis, and automation.

Explore

SQL

Learn SQL to manage and query relational databases.

Explore

PHP

Master PHP to build dynamic and secure web applications.

Explore

Popular tools

Helpful utilities for quick tasks.

Browse tools

Text Diff Checker

Compare two pieces of text to find differences.

Use tool

AES Encryption/Decryption

Encrypt and decrypt text using AES encryption.

Use tool

Random Name Generator

Generate realistic names with customizable options.

Use tool

Keyword Density Checker

Analyze keyword density for SEO optimization.

Use tool

JSON Formatter & Validator

Beautify, minify, and validate JSON data.

Use tool

Latest articles

Fresh insights from the CodiWiki team.

Visit blog

AI in Drug Discovery: Accelerating Medical Breakthroughs

In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…

Read article

AI in Retail: Personalized Shopping and Inventory Management

In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …

Read article

AI in Public Safety: Predictive Policing and Crime Prevention

In the realm of public safety, the integration of Artificial Intelligence (AI) stands as a beacon of innovati…

Read article

AI in Mental Health: Assisting with Therapy and Diagnostics

In the realm of mental health, the integration of Artificial Intelligence (AI) stands as a beacon of hope and…

Read article

AI in Legal Compliance: Ensuring Regulatory Adherence

In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…

Read article

Need help implementing this?

Get senior engineering support to ship it cleanly and on time.

Get Implementation Help