Ad

Laravel Old For Selectbox Options

- 1 answer

So i'm working on a standalone laravel project (still a bit new to this..). Using the same form blade file adding and editing records. I've set old to the fields for validation checks, however i can't seem to get old working correctly for dropdowns. The select options are loaded in a foreach, and ive got a check to set it to selected if the record on edit matches the dropdown option selected, however when i add the old method to it, select a new option (E.G. "broker") and force validation to throw an error it works as expected & saves whatever i've selected (option "broker"), it saves the option on submit, but then when i go to edit another record (option "assessor") it displays the same option i previously selected (option "broker")...if any of that made sense!

Here's the foreach with option select from the blade file:

<select class="form-control" name="other_party_details[party_type_id]" id="other_party_details[party_type_id]">
<option value="">--- Please Select ---</option>
@foreach($partyTypes as $partyType)
<option value="{{ $partyType->id }}" {{old('other_party_details.party_type_id', !empty($claim->otherParty->party_type_id)) == $partyType->id ? 'selected' : '' }}>
{{ $partyType->label }}
</option>
@endforeach
</select>

What am i doing wrong?

I've exhausted google as for the most it seems to show results for multi select (mine is not), or using LaravelCollective (which i'm not), so running out of places to look!

Thanks in advance

Ad

Answer

You can try below code

<option value="{{$partyType->id}}" @if(old('party_type_id') == $partyType->id || $claim->otherParty->party_type_id == $partyType->id ) selected @endif> {{ $partyType->label }}</option>
Ad
source: stackoverflow.com
Ad