I may be over complicating this or missing something very simple so forgive me if I am.
I am trying to manipulate/map some data in my model.
A very simple example of what I am trying to do:
I have $model->attr, this will return whatever is in my database for that column.
I know that the return will be 1, 2 or 3.
When displaying on a view I would like this value to show something different, for the sake of this example lets say 1=>'red', 2=>'blue', 3=>'green'.
How would I go about doing this in the model? I have had a look at Accessors & Mutators but not sure if they are the right thing to use.
Thanks in advance.
You would need an accessor indeed:
public function getNewValueAttribute($value){
switch($value) {
case '1':
return 'red';
case '2':
return 'blue';
case '3':
return 'green';
}
}
Put that inside your model and it should work perfectly!
Laravel converts the attribute to CamelCase, so $user->full_name has an accessor getFullNameAttribute, $user->all_your_base_are_belong_to_us becomes getAllYourBaseAreBelongToUsAttribute.
I think you are trying to output text based on conditions. If it is you can try
@if($model->attr==1)
red
@elseif($model->attr==2)
blue
@else
green
@endif