Support the ongoing development of Laravel.io →
Views Blade Forms

First and foremost, i'm very green in Laravel. So pls take that into consideration if i use any wrong terminlogy or dont grasp some stuff that should be obvious to you all. I'm trying to populate a dropdown list with the results of database query.

Model for Dropdown Entries:

    Class Dropdown {

           public static function getFilters() {
                  $filter = DB::select(DB::raw('SELECT DISTINCT filter AS filt
                                FROM filter.database'));
                  return $filter;
           }

     }

Controller:

    class FilterController extends BaseController {
           public function getSql_Test() {
                $dropdown = Dropdown::getFilters();
                return View::make('Dropdown.show', ['Dropdown' =>$dropdown]);
            }
    }

What my controller accomplishes right now is to display the results of the query (which i want to populate the dropdown) on the page. Can someone pls help me on how to pass this entries onto the select tag here and show it on my View?

Last updated 3 years ago.
0

Documentation for dropdown is provided @ http://laravel.com/docs/4.2/html#drop-down-lists

You are already passing the data to the view. In your view/blade file, you can do something like this


Form::select('name', $Dropdown );

Last updated 3 years ago.
0

The problem with this is that, the select parameter expects a string. What i'll be passing on to it as per your suggestion will be an object.

Last updated 3 years ago.
0

Nope. Select statement accepts array as second argument. Since query builder returns the value as object, you have to remap to array before passing it select.


$filter = DB::select(DB::raw('SELECT DISTINCT filter AS filt FROM filter.database'));

foreach ($filter as $key => $value) { 
  $dropdown[$key] = $value->filt; 
}

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

Tony-254 tony-254 Joined 10 Nov 2014

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.