Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

489
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda