How to use function old() in Blade template to get the last element of array
P粉309989673
2023-09-02 22:00:20
<p>How to get the last element of the array 'g3' inside the old() function without knowing the number of elements. </p>
<pre class="brush:php;toolbar:false;"><select name="g3[]" multiple="multiple">
<option value="1" @if (old('g3')=="1" ) {{ 'selected' }} @endif >lifting</option>
<option value="2" @if (old('g3')=="2" ) {{ 'selected' }} @endif >jogging</option>
<option value="3" @if (old('g3')=="3" ) {{ 'selected' }} @endif >sleeping</option>
</select>
<div {!! old('g3') != 3 ? '':' style="display: none"' !!}> Not to be seen</div></pre>
<p>How to get the selected item within a div. </p>
As mentioned by @apokryfos in the comments:
Additional instructions
Based on your comment, the following demo should be sufficient:
If your
old
value isarray
, you can usein_array
instead.Check if
old('g3')
exists, then check ifvalue
is in the arrayold('g3')
How to get the last element of the array, you can try this
array_values() function returns an array containing all the values of the array.
Tip: The returned array will have numeric keys, starting from 0 and increasing gradually.