Skip to content

Instantly share code, notes, and snippets.

@tsolar
Last active August 29, 2015 14:06
Show Gist options
  • Save tsolar/ea08c04908a957f0555f to your computer and use it in GitHub Desktop.
Save tsolar/ea08c04908a957f0555f to your computer and use it in GitHub Desktop.
Base Model for Laravel 4
<?php
use LaravelBook\Ardent\Ardent; // it can be Eloquent too
class BaseModel extends Ardent {
protected $_name;
public static function optionList($attribute = 'name') {
$model = static::getModel();
$name = $model->_name;
$lists = $model::lists($attribute, 'id');
return array('' => "Choose {$name}...") + $lists;
}
public static function optionsList($attribute = 'name') {
return static::optionList($attribute);
}
public static function optionListExcluding($excludes = array(), $attribute = 'name') {
$lists = static::optionList($attribute);
if (!empty($excludes)) {
foreach ($excludes as $id) {
if (isset($lists[$id])) {
unset($lists[$id]);
}
}
}
return $lists;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment