简体   繁体   中英

How to call a foreach select box in controller to a blade view with laravel?

I am using laravel 5.2 and I have a field which is a selectbox, and I call it in the controller to show in my view. But The value of the selectbox does not appear. When my code in the controller like this:

foreach($getDataListPengikut as $dtListPengikut):
        $msg["opt"] .= '<tr>
            <td><select class="form-control" id="name_{{ $dtListPengikut->KODE }}" name="name[]" data-live-search="true" style="width:100%">
                <option value=""> --Silahkan Pilih-- </option>
                    @foreach ($getPengikut as $getPeng)
                        <option value="{{ $getPeng->KODE }}">{{ $getPeng->DESKRIPSI }}</option> 
                    @endforeach
                </select>
            </td> 
            <td><input type="text" value="'.$dtListPengikut->KETERANGAN.'" id="ket_'.$dtListPengikut->KODE.'" name="ket[]" placeholder="Keterangan" class="form-control ket_list" style="text-transform:uppercase" required="false" />
            </td>    
            <td><button type="button" name="add" id="add2" class="btn btn-success"><b>+</b></button>
            </td>
        </tr>';
    $no++;
    endforeach;

In the inside of my selectbox is {{ $getPeng->DESKRIPSI }}

And when I change my code into like this:

<select class="form-control" id="name_'.$dtListPengikut->KODE.'" name="name[]" data-live-search="true" style="width:100%">
<option value=""> --Silahkan Pilih-- </option>
'. foreach($getPengikut as $getPeng) .'
    <option value="'. $getPeng->KODE.'"> '.$getPeng->DESKRIPSI.' </option> 
'.endforeach.'
</select>

I got an error :

syntax error, unexpected 'foreach' (T_FOREACH)

You're using the wrong syntax. Use {{ expression }} instead of "'. . The correct syntax is:

<select class="form-control" id="name_{{ $dtListPengikut->KODE }}" name="name[]" data-live-search="true" style="width:100%">
<option value=""> --Silahkan Pilih-- </option>
    @foreach ($getPengikut as $getPeng)
        <option value="{{ $getPeng->KODE }}">{{ $getPeng->DESKRIPSI }}</option> 
    @endforeach
</select>
In the view file :

<select class="form-control" id="name_{{ $dtListPengikut->KODE }}" name="name[]" data-live-search="true" style="width:100%">
 <option value=""> --Silahkan Pilih-- </option>
  @foreach ($getPengikut as $getPeng)
        <option value="{{ $getPeng->KODE }}">{{ $getPeng->DESKRIPSI }}</option> 
  @endforeach
</select>


In in the controller:
$html = '<select class="form-control" id="name_'.$dtListPengikut->KODE.'" name="name[]" data-live-search="true" style="width:100%">
    <option value=""> --Silahkan Pilih-- </option>';
    foreach($getPengikut as $getPeng):
        $html .= '<option value="'. $getPeng->KODE.'"> '.$getPeng->DESKRIPSI.'</option>' ;
    endforeach;
$html .='</select>';

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM