Passing variable from component into a slot
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
# Passing Variables from Component into Slots: Mastering Context in Blade Templates
As developers working with modern front-end frameworks and server-side templating like Laravel Blade, understanding how data flows between parent components and nested slots is crucial. This often comes down to managing variable scope and context within your view definitions.
The question we are addressing today is: **Is it possible to pass a variable from a component into a slot?** And if so, how do we correctly access that data inside the slot when iteration is involved?
Let's dive into the specifics, analyze the provided scenario, and establish the correct pattern for passing dynamic data into flexible component slots.
---
## The Challenge: Context and Scope in Slot Definitions
The example you provided highlights a common point of confusion when dealing with nested Blade components and `@slot` directives:
```blade
{{-- index.blade.php --}}
@component('slider', ['entities' => [0, 1, 2]])
@slot('title')
Slider title
@slot('slide')
Slider content no {{ $entity }} <-- Error occurs here
@endslot
@endcomponent
```
The issue arises because the `$entity` variable, which is generated during a loop in the parent view (`index.blade.php`), exists only within that specific loop scope. When it is passed into the slot definition in the component, the slot itself doesn't automatically inherit the context of the surrounding iteration unless explicitly handled. The exception `Undefined variable: $entity` confirms that the variable from the outer scope is not available within the inner slot's execution context by default.
## The Solution: Explicit Context Passing and Iteration Control
The key to solving this is recognizing that slots are containers for *content*, and they need access to the data provided by the component instance. We cannot rely on implicit variable inheritance across structural boundaries like `@slot` definitions. Instead, we must ensure the iteration context is available where needed.
The most robust solution involves passing the necessary data directly into the slot definition or structuring the iteration differently within the component itself.
### Method 1: Passing Iteration Data Directly (Recommended)
Instead of trying to access an outer loop variable inside the slot, pass the entire collection or the specific item you want to render directly into the slot's context. This shifts the responsibility of iteration management back to the component where it belongs.
**Revised Approach:** Pass the necessary data structure into the slot and let the component handle the looping.
#### Example Implementation:
**1. Parent View (index.blade.php):** We pass the full entities array as a context variable.
```blade
@component('slider', ['entities' => [0, 1, 2]])
@slot('title')
Slider title
{{-- Pass the specific entity data directly into the slide slot --}}
@slot('slide', $entity)
@endslot
@endcomponent
```
**2. Component View (slider.blade.php):** The component now receives `$entity` directly in its slot, making it available for use.
```blade
{{ $title }}
-
{{-- Now $entity is defined and accessible here --}}
- {{ $slide }}