Q:

How to showing value on dropdown list form auto select value from database

i have a from to edit/ update data . on colomn input like name ,email ,date i dont have a problem because this value can e displayed at this colomn like this :
 <div class="item form-group">
                    <label class="control-label col-md-3 col-sm-3 col-xs-12" for="nama">NAMA <span class="required">*</span>
                    </label>
                    <div class="col-md-6 col-sm-6 col-xs-12">
                    <input id="nama" type="text" class="form-control @error('nama') is-invalid @enderror" name="nama" value="{{$pns->users->nama}}" required autocomplete="nama">

                        @error('nama')
                            <span class="invalid-feedback" role="alert">
                                <strong>{{ $message }}</strong>
                            </span>
                        @enderror
                    </div>
                  </div>

this value on this input is value="{{$pns->users->nama}}"  and its still working , but how i can do this on this input ? this input is select dropdown list like this :
 <div class="form-group">
                      <label class="control-label col-md-3 col-sm-3 col-xs-12">Agama <span class="required">*</span></label>
                      <div class="col-md-6 col-sm-9 col-xs-12">
                        <select name="agama_id" id="agama_id" class="form-control">
                            @foreach($agama as $masters => $agamas )
                            <option value="{{ $masters }}">{{ $agamas }}</option>
                            @endforeach
                        </select>
                      </div>
                    </div>

where i can user this value like {{$pns->users->nama}} ??? if i open this update form , its will choose automaticlly  like this db ??
someone have solution ?

A:

Try this. 
<select name="agama_id" id="agama_id" class="form-control">
    @foreach($agama as $masters => $agamas )
         <option value="{{ $masters }}" @if ($pns->users->agama_id == $masters ) selected @endif>{{ $agamas }}</option>
    @endforeach
</select>

