PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, January 26, 2022

[FIXED] Setting selected option in laravel from database

 January 26, 2022     eloquent, laravel, laravel-4, laravel-5     No comments   

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
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

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

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing