Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

487
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!