Skip to content

Commit

Permalink
Add getFirstWhereStartsWith() to ComponentAttributeBag
Browse files Browse the repository at this point in the history
Following up on the changes from earlier today, in order to effectively use this with Livewire we need to be able to get the attribute value that starts with 'wire:model' so we can use that value for the error bag, among other reasons.

This function gives you the ability to do that. Your component would look like this:

Usage
```html
<x-text-input wire:model.lazy="name" />
```

Blade Component
```php
<div>
    <input type="text" {{$attributes->whereStartsWith('wire:model')}} />
    @error($attributes->getFirstWhereStartsWith('wire:model'))
        {{$message}}
    @enderror
</div>
```
  • Loading branch information
iAmKevinMcKee authored Jun 27, 2020
1 parent 07ee3e8 commit 1a0c7b8
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/Illuminate/View/ComponentAttributeBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,30 @@ public function thatStartWith($string)
{
return $this->whereStartsWith($string);
}

/**
* Get the first attribute that starts with the given value from the attribute array.
*
* @param string $string
* @param mixed $default
* @return mixed
*/
public function getFirstWhereStartsWith($string, $default = null)
{
return $this->whereStartsWith($string)->getIterator()->current() ?? value($default);
}

/**
* Get the first attribute that starts with the given value from the attribute array.
*
* @param string $string
* @param mixed $default
* @return mixed
*/
public function getFirstThatStartsWith($string, $default = null)
{
return $this->getFirstWhereStartsWith($string, $default);
}

/**
* Exclude the given attribute from the attribute array.
Expand Down

0 comments on commit 1a0c7b8

Please sign in to comment.