1

I have my users table with a column named status. In this column I just store a number, based off their status, 1 - admin, 2 - owner, etc. I know that I can display these values with {{ users->status }}, but only shows the stored number. How can I show the actual name instead of the stored number?

0

1 Answer 1

4

Use Eloquent Accessors & Mutators.

Define an array of statuses and use the status number from your database as a key to change the value in the model so that in the view {{ $users->status }} equals admin

class User extends Eloquent {

    protected $statuses = array(
        '1' => 'admin',
        '2' => 'owner'
    );

    public function getStatusAttribute($value)
    {
        return $this->statuses[$value];
    }

}
  • getStatusAttribute converts 1 to admin when retrieving a record.


You could also have a set method for when records are stored in the model.

In View {{ $user->status }} // echos 'admin'

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.