简体   繁体   中英

Laravel how to populate select box from database

i have a database table that store list of country,now am trying to get the list of country in a dropdown form,but is not working, maybe am missing something.this is my controller code

` public function details($id){
        //fetch post data
        //$post = Post::find($id);

        $country_options = Country::pluck('country', 'id');
        $country_options = DB::table('regions')->orderBy('country', 'asc')->pluck('country','id');

        //pass posts data to view and load list view
        return view('dashboard')->with('country_options',$country_options);

    }`

and the code that echo the dropdown in my form look like this ` '

@foreach($countries as $country)'+
               '<option value="{{ $country->id }}"> {{ $country->country }}</option>'+
               '@endforeach'+`

and my route looks like this

Route::get('/Business', 'BusinessController@details')->name('Business');

but i keep getting this error message

Undefined variable: countries (View: C:\xampp\htdocs

have made research but could not fine the solution. any help would be appreciated with proper documentation/explanation.thanks

Your mistake is that your passing one variable name to your view and trying to read with another name. I made some quick example for you. Here my table is "Pais" ("Country" in portuguese), and use an clorure route for the callback function. (Lavarel 5.4)

Route (web.php):

Route::get('/test', function () {
    $countries = \App\Pais::all();
    return view('test_stackoverflow')->with(['countries' => $countries]);
});

View (test_stachoverflow.blade.php):

<select name="countries" id="countries">
  @foreach($countries as $country )
    <option value="{{ $country->id }}">{{ $country->name }}</option>
  @endforeach
</select>

Result


Another option, if you want to stay with the pluck method:

Route (web.php):

Route::get('/test', function () {
  /* $countries = \App\Pais::pluck('name','id'); */
  // or
  $countries = DB::table('pais')->pluck('name','id');
  return view('test_stackoverflow')->with(['countries' => $countries]);
});

View (test_stachoverflow.blade.php):

  <select name="countries" id="countries">
    @foreach($countries as $id => $country )
      <option value="{{ $id }}">{{ $country }}</option>
    @endforeach
  </select>

Both solutions have the same result in the generated HTML.

Hope this can help you! Best regards.

You are passing country_options from your controller not countries

Try this

@foreach($country_options as $country)
    <option value="{{ $country->id }}">
        {{ $country->country }}
    </option>
@endforeach

first you dont need $id if you want to gell all categories

relace controller code with:

public function details(){
    $country_options = Country::All();
    return view('dashboard',compact('countries'));
}

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