Skip to content

Instantly share code, notes, and snippets.

@gms8994
Created September 26, 2013 21:21
Show Gist options
  • Save gms8994/6720734 to your computer and use it in GitHub Desktop.
Save gms8994/6720734 to your computer and use it in GitHub Desktop.
Laravel Select with data- Attributes
Form::macro('selectWithAttribs', function($name, $options, $default = null) {
$opt = array();
$select = '<select name="' . $name . '">';
foreach ($options as $option) {
$opt_node = '<option value="' . $option->value . '"';
$props = $option->getAttributes();
unset($props['value'], $props['key']);
$prop_value = array();
foreach ($props as $prop => $id) {
$prop_value[] = 'data-' . $prop . '="' . $option->$prop . '"';
}
$opt_node .= join(" ", $prop_value);
$opt_node .= '>' . $option->key . '</option>';
$opt[] = $opt_node;
}
$select .= join('', $opt);
$select .= '</select>';
return $select;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment