Let's imagine I have a blade component such as:
<input type="text" id="foo" name="{{$name}}"/>
<script>
$('#foo').whatever
</script>
IRL, this component is far more complicated. You could imagine a long form with plenty of fields with generic names like name
, description
, owner
...
Then I use this component multiple times, again I propose a foreach
but we could imagine something else in which I cannot use an iterator to provide an id to my component:
@foreach($items as $item)
@component('foo')
@endcomponent
@endforeach
How can I ensure the uniqueness of the id
?
One perhaps bad solution would be to use a local variable:
@php($id = uniqid())
<input type="text" id="{{$id}}" name="{{$name}}"/>
<script>
$('#{{$id}}').change(whatever)
</script>
Is there a better way?
#foo--1
#foo--2
etc.$loop->iteration
laravel.com/docs/5.8/blade – Tungstite