Could anyone help me to understand how to use variables in the following case:
@for($i = 0; $i < 3; $i++)
@if(isset($images->image_$i_url))
<div class="w-25" >
<img src="{{ Storage::url($images->image_$i_url) }}" alt="image" style="width: 128px;">
</div>
@endif
<div class="form-group">
<div class="input-group">
<div class="custom-file">
<input name="product_image_{{ $i }}" type="file" class="custom-file-input" id="product_image_{{ $i }}">
<label class="custom-file-label" for="exampleInputFile">Choose file</label>
</div>
</div>
@error("product_image_{{ $i }}")
<div class="text-danger">{{ $message }}</div>
@enderror
@endfor
It doesn’t work for $i
3
Answers
If I get correctly, you have a problem with this line:
Particularly:
Which is not solved to:
Technically, you cannot put $ symbols as part of property name when you call it.
Therefore, a trick is by using ‘{}’
Full line example:
You just want to iterate on three images?
Try this instead. Loop through the collection of images. Whenever you want to reference the index of the loop, use $loop->index. This will scale better than using a hard-coded for loop.