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…
Section overview
5 resourcesCovers 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
- Create a controller and pass a list of items to a view. Display these items in the view using Blade syntax.
- Share a variable across multiple views. Try to access this shared variable in different views.
Solutions
- Controller:
public function show()
{
$items = ['Apple', 'Banana', 'Cherry'];
return view('items', compact('items'));
}
View:
<ul>
@foreach ($items as $item)
<li>{{ $item }}</li>
@endforeach
</ul>
- 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.
Related topics
Keep learning with adjacent tracks.
Popular tools
Helpful utilities for quick tasks.
Latest articles
Fresh insights from the CodiWiki team.
AI in Drug Discovery: Accelerating Medical Breakthroughs
In the rapidly evolving landscape of healthcare and pharmaceuticals, Artificial Intelligence (AI) in drug dis…
Read articleAI in Retail: Personalized Shopping and Inventory Management
In the rapidly evolving retail landscape, the integration of Artificial Intelligence (AI) is revolutionizing …
Read articleAI 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 articleAI 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 articleAI in Legal Compliance: Ensuring Regulatory Adherence
In an era where technology continually reshapes the boundaries of industries, Artificial Intelligence (AI) in…
Read article