Generate a Select box from Collection

Submitted by xLink - 10 years ago

Use this to generate a select box from a Eloquent Collection

Form::macro('DBSelect', function($fieldName, $collection, $options=array('id' => 'name')){
	$key = key($options);
	$rows = $collection->lists( $options[ $key ], $key );

	return Form::select($fieldName, $rows);
});


// Usage:
<?php
$categories = Category::all();
?>
{{ Form::DBSelect('categories', $categories) }} // make it generate from ID & Name
{{ Form::DBSelect('categories', $categories, array('id' => 'slug')) }} //make it use the slug for the name instead