Wednesday, January 26, 2022

[FIXED] Setting selected option in laravel from database

Issue

I have a problem here, I want to make an edit form, I want the province that appears in the edit form to be the province selected from the database, here my code

  <select name="provinsi" id="provinsi" class="form-control">
                <option value="">Pilih Provinsi</option>
                @foreach ($province as $prov)
                <option value="{{$prov->name}}">{{$prov->name}}</option>
                @endforeach
            </select>

here my controller code

public function ShowSantri(Request $request,$id)
    {

        $province = DB::table('provinces')->get();
        $profiles = Profile::find($request->id);

        return view('admin/modal/edit_santri',compact('profiles','province'));

Solution

You can try like this:

<select name="provinsi" id="provinsi" class="form-control">
    <option value="">Pilih Provinsi</option>
    @foreach ($province as $prov)
         <option value="{{$prov->name}}"
             @if ($profiles['provName'] == $prov->name)
                 selected
             @endif>
             {{$prov->name}}
         </option>
    @endforeach
</select>

Just I'm not sure what is name in your $profiles for province. But I think that is what you need.

Here and here are some examples.

Good luck!



Answered By - mare96

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.