PHP code example of abdelhamiderrahmouni / blade-component-scopes
1. Go to this page and download the library: Download abdelhamiderrahmouni/blade-component-scopes library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
abdelhamiderrahmouni / blade-component-scopes example snippets
<label {{ $attributes->scope('label')->merge(['class' => 'text-gray-500']) }}>
<x-forms.input name="first_name"
:label="__('Hello')"
label:class="flex flex-col"
label:for="#name-input"
input:id="name-input"
container:id="name-input-container" />
// forms.input.blade.php
<div {{ $attributes->scope('container')->merge(['class' => 'flex gap-y-2']) }}>
@if($label)
<label {{ $attributes->scope('label')->merge(['class' => 'text-gray-500']) }}>
{{ $label }}
</label>
@endif
<input {{ $attributes->scope('input')->merge(['class' => 'border-b']) }} />
</div>
// AppServiceProvider.php
use Illuminate\View\ComponentAttributeBag;
public function register(): void
{
ComponentAttributeBag::macro('scope', function (string $scope): ComponentAttributeBag {
$prefix = $scope . ':';
$prefixLength = strlen($prefix);
$scopedAttributes = [];
foreach ($this->getAttributes() as $key => $value) {
if (str_starts_with($key, $prefix)) {
$scopedAttributes[substr($key, $prefixLength)] = $value;
}
}
return new ComponentAttributeBag($scopedAttributes);
});
}