如果我在数据库中有这样的表:
users
id
username
status
状态可以是:
状态0-未激活1-活动2-禁止
我应该把状态放在哪里,我有一些可能性:
- 用户的存储库中模式
- 在User Eloquent类中
- 在助手中
- 在数据库中(有时我会为此创建50多个表,但我认为这不是个好主意)
- 在控制器构造函数中,并像"View::share('select',$usersStatuss);"那样共享它,我认为这不是一个干净优雅的解决方案
编辑:
{!!Form::select('status',user_statuses(),@$user->status,array('class'=>'Form-control'))!!}
看看函数"user_statuses()",它应该在哪里以及如何获取数据?
我建议2,就像这个
class User extends Model
{
const STATUS_BANNED= 2;
const STATUS_INACTIVE = -1;
const STATUS_ACTIVE = 1;
}
模型访问属性,如User::STATUS_ACTIVE,这是清楚的。
好的,我现在明白了。IMHO,最好的方法是使用带有静态函数的helper类来获取用户状态。
Class UserHelper
{
public static function getStatuses() {
return [
0 => not_active,
1 => active,
2 => banned
];
}
}
UserHelper::getStatuses();
优点:
- 获取状态不需要初始化User对象
- 您也可以在控制器和视图中使用它