为拉拉维尔中的动态 div 选择了选项



在编辑的情况下,我正在通过 for 循环生成 4 个 HTMLdiv :

@for($i=0;$i < 4;$i++)
    <div class="col-lg-12 no-padding">
        <div class="form-group">
            <select name="eng_country[]" id="eng_country" class="form-control profile-engform new-font addlisting_classic" placeholder="Country">
                <option value=""> Country </option>
                @foreach($result['total_exp_country'] as $key=>$value)
                    <option>{{$value->country_eng}}</option>
                @endforeach
            </select>
        <span class="text-danger text_error" id="eng_country_error"></span>
        </div>
    </div>
@endfor

在此内容中,我正在填充国家/地区列表一个选项。

我在该变量结果中获得了选定的国家/地区列表,例如:

array:2 [▼
  0 => {#354 ▼
    +"id": "1"
    +"worker_id": "42"
    +"country_id": "1"
    +"exp_year": "2"
    +"added_date": "2018-01-30 00:00:00"
    +"country_eng": "India"
    +"country_arb": "الهند"
    +"ordering": "2"
  }
  1 => {#355 ▼
    +"id": "2"
    +"worker_id": "42"
    +"country_id": "2"
    +"exp_year": "1"
    +"added_date": "2018-01-31 00:00:00"
    +"country_eng": "Srilanka"
    +"country_arb": "سيريلانكا"
    +"ordering": "1"
  }
]

我的问题是我想选择选项值,无论我从这个变量中得到什么,如果它为空,则不应选择任何值

values:


After  <?php echo "<pre>";print_r($result['total_exp_country']);exit;  ?>
Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [country_eng] => India
            [country_arb] => الهند
            [ordering] => 2
            [added_date] => 2018-01-30 00:00:00
        )
    [1] => stdClass Object
        (
            [id] => 2
            [country_eng] => Srilanka
            [country_arb] => سيريلانكا
            [ordering] => 1
            [added_date] => 2018-01-31 00:00:00
        )
    [2] => stdClass Object
        (
            [id] => 3
            [country_eng] => UAE
            [country_arb] => الأمارات العربية المتحدة
            [ordering] => 2
            [added_date] => 2018-01-29 00:00:00
        )
)

And array data for : $result['exp_country']

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [worker_id] => 42
            [country_id] => 1
            [exp_year] => 2
            [added_date] => 2018-01-30 00:00:00
            [country_eng] => India
            [country_arb] => الهند
            [ordering] => 2
        )
    [1] => stdClass Object
        (
            [id] => 2
            [worker_id] => 42
            [country_id] => 2
            [exp_year] => 1
            [added_date] => 2018-01-31 00:00:00
            [country_eng] => Srilanka
            [country_arb] => سيريلانكا
            [ordering] => 1
        )
)

I want to match id with country id for selected option ...

试试下面的代码:

@foreach($result['total_exp_country'] as $key=>$value)
    <option 
        @if($result['exp_country'][$key]['country_id']==$value->country_id) 
            selected 
        @endif 
        value="{{$value->country_id}}">{{$value->country_eng}}
    </option>
@endforeach

最新更新