Last active
December 17, 2015 16:59
-
-
Save fhferreira/5643232 to your computer and use it in GitHub Desktop.
Function for fill array_data for use with Form::select in Laravel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 <[email protected]> | |
**/ | |
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