2
votes

If I have table in database like this:

users
    id
    username
    status

and status can be:

status 0 - not active 1 - active 2 - banned

where should I put statuses, i have some possibilities:

  1. in repository pattern for users
  2. in User Eloquent class
  3. in helpers
  4. in database (sometimes I would create 50 more tables for this and I don't think this is good idea)
  5. in controller constructor and share it like "View::share('select', $usersStatuses);" i think this is not clean and elegant solution.

?

EDITED:

{!! Form::select('status', user_statuses(), @$user->status, array('class' => 'form-control')) !!}

Look at function "user_statuses()" where and how this should get data ?

2

2 Answers

0
votes

I suggest 2, and like this

class User extends Model
{
    const STATUS_BANNED= 2;
    const STATUS_INACTIVE = -1;
    const STATUS_ACTIVE = 1;
}

Model access property like User::STATUS_ACTIVE, it is clear.

0
votes

Ok, I understand now. IMHO, the best approach is helper class with static function for getting user statuses.

Class UserHelper
{
    public static function getStatuses() { 
        return [
              0 => not_active, 
              1 => active,
              2 => banned
        ];
    }
}

UserHelper::getStatuses();

Pros:

  • You don't need initialize User object for getting statuses.
  • You can use it in the Controller and View too.