0

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?

1 Answer 1

1

That is very easy to do, and fun (cause its easy)!

$filter = DB::table('filter.database')->select(DB::raw('DISTINCT(filter) AS filt'))
    ->lists('filt');

This will give you an array where the key is an integer and the value is your filter.

$filter = DB::table('filter.database')->select(DB::raw('DISTINCT(filter) AS filt'))
    ->lists('filt', 'filt');

This will give you an array where the key is filt and the value is filt.

you can push it into the view by extending your view with the variable like so:

return View::make('Dropdown.show')->with('dropdown', $dropdown);

Now you can make your select and just push in the array where the value should be:

echo Form::select('size', $dropdown);

More information can be found here:

http://laravel.com/docs/4.2/queries#selects

As a tip, stay on the laravel docs. They are a treat to work with.

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a bunch for your input mate. I'm however getting the error message Call to a member function lists() on a non-object. Any tips?
I think it ha to do with using the eloquent manner. I have changed a few things. Should work
You're right. It is the way i wrote my queries. It works now. Thanks a lot man!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.