Passing Data and Sharing Variables

Tutorial 2 of 5

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.