Share Parent Data with Blade @aware
When building reusable Blade components, you sometimes need a child component to know which properties were passed to its parent.
For example, imagine a menu component where the parent defines a theme, and every menu item should automatically use that theme.
Instead of passing the same property to every child, Laravel provides the @aware directive.
Parent Component
Create a menu component:
resources/views/components/menu/index.blade.php
@props(['color' => 'blue'])
<ul class="menu-{{ $color }}">
{{ $slot }}
</ul>
Now you can use it like this:
<x-menu color="purple"><x-menu.item>Dashboard</x-menu.item><x-menu.item>Settings</x-menu.item><x-menu.item>Profile</x-menu.item>
</x-menu>
But the child components don't automatically receive the parent's color property.
Use @aware in Child Components
Create:
resources/views/components/menu/item.blade.php
@aware(['color' => 'blue'])
<li class="text-{{ $color }}-800">
{{ $slot }}
</li>
Now the child component can access the color property passed to its parent.
You don't need to pass color separately to every menu item.
Real-World Example
This approach is useful when creating reusable component systems.
For example, you can build:
- Tabs with a shared visual style
- Dropdown menus with common settings
- Card groups with shared layouts
- Navigation menus with configurable themes
- Form components with shared configuration
Important Detail
@aware can access data explicitly passed to the parent component through its HTML attributes.
It cannot access a default value defined only inside the parent's @props declaration if that value wasn't explicitly passed.
For example:
<x-menu>
<x-menu.item>Dashboard</x-menu.item>
</x-menu>
The child's @aware default value will be used rather than automatically inheriting the parent's default @props value.