Passing variable from component into a slot

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company
# 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 }}
``` By passing `$entity` explicitly via `@slot('slide', $entity)`, we ensure that the variable exists within the scope where the slot content is being rendered, resolving the undefined variable error. This pattern promotes cleaner separation of concerns, which aligns with best practices seen in complex application development, much like the robust architecture promoted by tools like those found on [Laravel Company](https://laravelcompany.com). ## Best Practices for Component Data Flow When building reusable components, think about data flow as a unidirectional pipeline. Avoid trying to force deep, implicit variable inheritance across nested view elements via slots alone when iteration is involved. 1. **Pass Objects, Not Just Primitives:** If the slot needs complex data (like an entity), pass the entity object itself rather than just its index or a single property. This makes the component more flexible and easier to test. 2. **Component Responsibility:** Let the child component handle its own rendering logic. If a slot requires iteration, the component receiving the slot should manage the loop over the data it was provided. 3. **Data Aggregation:** For complex scenarios, consider passing an array of data into the slot and let the parent view iterate over that array *before* invoking the component, rather than trying to pass individual loop variables deep into the slot structure. ## Conclusion Passing variables from a parent component into a slot is entirely possible, but it requires careful management of scope. The initial failure occurred because standard Blade syntax does not automatically propagate iteration context across `@slot` boundaries. By explicitly passing the required data (like `$entity`) directly into the slot definition, we establish a clear and predictable data pipeline. This approach ensures that your components remain decoupled, maintainable, and adhere to solid architectural principles, making complex UI assembly much smoother when developing applications with Laravel.