php - Laravel how to populate select box from database -


i have database table store list of country,now trying list of country in dropdown form,but not working, maybe missing something.this 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 view , load list view         return view('dashboard')->with('country_options',$country_options);      }` 

and code echo dropdown in form ` '

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

and route looks

route::get('/business', 'businesscontroller@details')->name('business'); 

but keep getting error message

undefined variable: countries (view: c:\xampp\htdocs 

have made research not fine solution. appreciated proper documentation/explanation.thanks

your mistake passing 1 variable name view , trying read name. made quick example you. here table "pais" ("country" in portuguese), , use clorure route 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 $country )     <option value="{{ $country->id }}">{{ $country->name }}</option>   @endforeach </select> 

result


another option, if want stay 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 $id => $country )       <option value="{{ $id }}">{{ $country }}</option>     @endforeach   </select> 

both solutions have same result in generated html.

hope can you! best regards.


Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -