Skip to content

Instantly share code, notes, and snippets.

@fhferreira
Last active December 17, 2015 16:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save fhferreira/5643232 to your computer and use it in GitHub Desktop.
Save fhferreira/5643232 to your computer and use it in GitHub Desktop.
Function for fill array_data for use with Form::select in Laravel
<?php
class Arrays{
/**
* Sample $obj = Users::all();
* @param Object(Eloquent) $obj
* Key usage for assoc with value in the array $data
* @param string $key
* Value usage for fill array $data
* @param string $value
* Enable first element
* @param boolean $select
* Parameter for first element in the array $data
* @param string $text
* Parameter for concat with value
* @param string $concat
* @return Array $data
* @author Flávio Henrique Ferreira <flaviometalvale@gmail.com>
**/
public static function montaCombobox( $obj, $key, $value, $select = FALSE, $text = 'Selecione', $concat = NULL)
{
$data = array();
if($select){
$data[''] = $text;
}
foreach($obj as $o){
if( is_null($concat) ){
$data[$o->{$key}] = $o->{$value};
}else{
$data[$o->{$key}] = $o->{$concat} . " - " . $o->{$value};
}
}
return $data;
}
}
/*Usage*/
$categories = Arrays::montaCombobox( Categories::select("id","description")->get() , "id", "description" , TRUE , "Select", "id");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment