Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

486
Vistas
Laravel - Manipulate data from model

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.

about 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

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.

about 4 years ago · Santiago Trujillo Denunciar

0

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
about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda