2
<div class="col-xs-12 col-sm-12 col-md-12">
            <div class="form-group">
                <strong>From Destination:</strong>
                <select name="from_destination_data" class="form-control">
                    @if ($destinationsData != [])
                        @foreach($destinationsData as $info)
                            <option value="{{$destinationsData[$index]}}">{{$info}}</option>
                        @endforeach
                    @endif
                </select>
            </div>
        </div>

I retrieve the $index of the selected $destinationData and when dd($destinationsData[$index]); I get the result that I need but it doesn't appear when I put it in the value as shown above

Sohaib
  • 1,972
  • 3
  • 9
  • 19
  • 1
    Sidenote: `@if ($destinationsData != []) ... @endif` is unnecessary unless you need to render something else in an `@else` block. `@foreach()` will naturally skip empty arrays/Collections. Next, the value should be `value="{{ $info }}` (I would think), what is `$index`? – Tim Lewis Jan 28 '20 at 20:00
  • 1
    thank you for the note, the index is like [0 => 'USA, FLIGHT', 1 => 'Canada, FLIGHT',....], so 0 and 1 are the indexes and I retrieve index =1, so I need when edit a row in my table, to show this data by this index first and the other data in the array after this data of this index. @TimLewis – Sohaib Jan 28 '20 at 20:07
  • Hmm... Still not sure what you mean. And I was more asking where does `$index` come from? When you're looping data with a `foreach()`, you generally don't need an index, but you can do `@foreach($array AS $index => $value)`, where `$array[$index]` would be the same as `$value`, but again, not generally necessary. – Tim Lewis Jan 28 '20 at 20:10
  • ok, I should show my controller to you to know what I mean about the index, let me explain in another scenario, let's say I have many types of car and I select Honda type, and I have a column name seats and I choose 4 seats, then after submit, I change my mind and want to choose 6 seats, not 4 when press edit the type of car should still Honda but it appears Mercedes so because there is not a value with the options, is it clear or we can chat to show you the controller? @TimLewis – Sohaib Jan 28 '20 at 20:17
  • 1
    That scenario is unfortunately more confusing... What's the relationship between type of car and seats? Why would 6 seats be Mercedes? How a select should work is that you have a list of options, you construct a bunch of ``, based on `$record->car_id == $option->id`. Is that clear? – Tim Lewis Jan 28 '20 at 20:21
  • so if I set the `value="$key"`, I don't know how to make this key as selected, I think this is what I should know, I wish to be solved, can you tell me how please @TimLewis – Sohaib Jan 28 '20 at 20:27
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/206818/discussion-between-sohaib-el-khatib-and-tim-lewis). – Sohaib Jan 28 '20 at 20:27
  • 1
    Assuming you had the `@foreach($destinationData AS $key => $info)`, it would be `value="{{ $key }}"`, then depending on how you save/retrieve the record you're editing, you'd set `` – Tim Lewis Jan 28 '20 at 20:38
  • this is what I need thank you, please put this comment like an answer to accept it :) @TimLewis – Sohaib Jan 28 '20 at 20:45
  • Sure, will do, gimme a sec. – Tim Lewis Jan 28 '20 at 20:45

1 Answers1

2

When constructing your <select> and <options>, you need to set a value for each that can be referenced when editing your record. See the following:

<div class="col-xs-12 col-sm-12 col-md-12">
    <div class="form-group">
        <strong>From Destination:</strong>
        <select name="from_destination_data" class="form-control>
          @foreach($destinationsData as $key => $info)
          <option value="{{ $key }}">{{ $info }}</option>
          @endforeach
        </select>
    </div>
</div>

Setting value="{{ $key }}" will, in this instance, set the value to a 0-based index, which can be referenced when editing:

@foreach($destinationsData as $key => $info)
<option value="{{ $key }}" {{ $record->from_destination_data == $key ? 'selected' : '' }}>{{ $info }}</option>
@endforeach

As long as $record->from_destination_data equals any of the $key iterations, that <option> will have selected="selected", setting it as the default selected option.

Tim Lewis
  • 27,813
  • 13
  • 73
  • 102
  • 1
    I suggest to make the value like this: `value="{{ $destinationsData [$key] }}"`, it causes a problem after submitting, the posted or the returned value is the key and not the data that I want but in this case, `value="{{ $destinationsData [$key] }}"` worked well, thank you by the way :) @TimLewis – Sohaib Jan 28 '20 at 21:38
  • 1
    Ah, in that case, you can just use `value="{{ $info }}"` and omit `$key` altogether. Remember that I said, when looping, the value of `$destinationsData[$key]` would be the same as `$info`? That's how array indices work in a `foreach()` loop :) Glad you got it working though – Tim Lewis Jan 28 '20 at 21:40
  • hello Tim. please can you help me to know how to add to this `{{ $record->from_destination_data == $key ? 'selected' : '' }}` else if, i want to make it as: `if(){}elseif(){}else{}`, can you help please, if you can i will add an question and you can answer it :). @TimLewis – Sohaib Jan 31 '20 at 16:30
  • You shouldn't need an `if/else if/else` for the example in this question... You can nest ternary operators, like `$condition ? ($condition2 ? 'something' : 'nothing') : 'nothing'`, but it's recommended against... Probably best to ask a new question instead of pinging me directly regardless. – Tim Lewis Jan 31 '20 at 16:35
  • see this https://stackoverflow.com/questions/60008199/use-ternary-operator-as-if-elseif-and-else @TimLewis – Sohaib Jan 31 '20 at 16:58