Looking to hire Laravel developers? Try LaraJobs

laravel-blade-bind-attributes maintained by pboivin

Description
Attribute unpacking for Laravel Blade component tags
Last update
2023/02/26 17:39 (dev-main)
License
Downloads
6
Tags

Comments
comments powered by Disqus

Attribute array unpacking for Laravel Blade component tags

⚠ 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.