pboivin/laravel-blade-bind-attributes

This package is abandoned and no longer maintained. No replacement package was suggested.

Attribute unpacking for Laravel Blade component tags

v0.2.0 2023-02-26 16:35 UTC

This package is auto-updated.

Last update: 2023-08-26 17:45:43 UTC


README

Build Status Latest Stable Version License

⚠ EXPERIMENTAL ⚠

This package adds support for @bind attributes in Blade component tags. The new attribute allows you to extract all keys from a given array as component props:

@php
    $header = [
        'title' => 'Lorem ipsum',
        'secondaryTitle' => 'Dolor sit amet',
    ];
@endphp

<x-header @bind="$header" class="my-header" />

This is equivalent to:

<x-header
    :title="$header['title']"
    :secondary-title="$header['secondaryTitle']"
    class="my-header"
/>

Requirements

  • PHP >= 8.1
  • Laravel >= 9.x

Installation

composer require pboivin/laravel-blade-bind-attributes

php artisan view:clear

Caveats

With class-based components, make sure to include a @props() directive in your template, even when you are defining the props on the component class:

app/View/Components/Header.php :

class Header extends Component
{
    public function __construct(
        public $title = 'Hello',
        public $secondaryTitle = 'World'
    ){}

    public function render()
    {
        return view('components.header');
    }
}

resources/views/components/header.blade.php :

@props([
    'title',
    'secondaryTitle',
])

<div {{ $attributes }}>
    <h1>{{ $title }}</h1>
    <h2>{{ $secondaryTitle }}</h2>
    {{-- ... --}}
</div>

Development

Test suite (phpunit)

composer run test

Code formatting (pint)

composer run format

License

This is open-sourced software licensed under the MIT license.