Laravel - Blade Templates - Using Attributes Slots Within Component Class
Blade components also allow you to access the component name, attributes, and slot inside the class's render method. However, in order to access this data, you should return a closure from your component's render
method. The closure will receive a $data
array as its only argument. This array will contain several elements that provide information about the component:
/**
* Get the view / contents that represent the component.
*
* @return \Illuminate\View\View|\Closure|string
*/
public function render()
{
return function (array $data) {
// $data['componentName'];
// $data['attributes'];
// $data['slot'];
return 'Components content';
};
}
The componentName
is equal to the name used in the HTML tag after the x-
prefix. So <x-alert />
's componentName
will be alert
. The attributes
element will contain all of the attributes that were present on the HTML tag. The slot
element is an Illuminate\Support\HtmlString
instance with the contents of the component's slot.
The closure should return a string. If the returned string corresponds to an existing view, that view will be rendered; otherwise, the returned string will be evaluated as an inline Blade view.