Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Laravel - Get old form input values for array-based input
<?php
@if(old('uid_tag'))
@foreach(old('uid_tag') as $product)
<input type="text" name="uid_tag[]" value="{{ old('uid_tag')[$loop->index] }}" size="30" />
<textarea
placeholder="Include weight or count"
name="product_description[]"
cols="45"
rows="1"
value="{{ old('product_description')[$loop->index] }}"
aria-required="true"
></textarea>
<input type="text" name="qty_ordered[]" value="{{ old('qty_ordered')[$loop->index] }}" size="3" />
<input type="text" name="unit_cost[]" value="{{ old('unit_cost')[$loop->index] }}" />
<input type="text" name="total_cost[]" value="{{ old('total_cost')[$loop->index] }}" />
@endforeach
@endif
@000kelvin

This comment has been minimized.

Copy link

@000kelvin 000kelvin commented Apr 15, 2020

Wouldn't this @if(old('uid_tag')) prevent the form elements from showing up if there are no old values present?

@000kelvin

This comment has been minimized.

Copy link

@000kelvin 000kelvin commented Apr 16, 2020

I'd suggest doing something like, (clean):

@foreach(old('uid_tag', ['value']) as $product)
    ...
@endforeach

This allows the form elements display at least once. Leaving ['value'] as [] returns the result of the code with the if statement.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment