1

I am using Laravel 5.5 & have Items table with more than 50 columns set as Tinynt to store values.

Item::where('id',$id)->first();

Now in my view I want to replace all integer values to corresponding String like

if($item->column1 == 1) { $value = 'String1'); }
if($item->column1 == 2) { $valuee = 'String2'); }

& so on. I know I can use Laravel Mutator but I have more than 50 columns & settings Mutator will not solve my issue. My problem is some related to this post but problem is number of columns.

Any solution please ?

6
  • 2
    Seems like this is a data issue. Why store all these in columns, as opposed to another table with joins?
    – Blue
    Commented Aug 8, 2018 at 7:42
  • 1
    Why are mutators not "good practice"? They seem perfect for what you're trying to achieve. If you don't want to go with the very obvious solution, at least explain why you think they don't match your use case.
    – Emile P.
    Commented Aug 8, 2018 at 7:43
  • 1
    Could make a mapping array 1=>'String1',2=>'String2' and use that if you insist on not using Mutators.
    – user8034901
    Commented Aug 8, 2018 at 7:45
  • Mutators are good, but in my case I have to define more than 50 Mutators with mapping array. Is there any simple solution ? else I have to go for Mutators. Commented Aug 8, 2018 at 7:49
  • I would suggest to store those mappings in a table instead. Commented Aug 8, 2018 at 7:51

3 Answers 3

1

I think 50 is the best way, but you can redefine __get() magic method:

//your Item model class

protected $castMutators = [
   //column1 => ['1' => 'string1', 2 => 'string2'],
   //column2 => ['1' => 'string1', 2 => 'string2'],
   //...
   //column50 => ['1' => 'string1', 2 => 'string2'],
];

public function __get($name)
{
   if (in_array($name, $this->castMutators)) {
         $this->{$name} = $this->castMutators[$name][$this->name]
   }

   parent::__get($name);
}
1

You can override the getAttributeValue() or setAttributeValue() function on the Eloquent model like below. Then you can use the same accessor or mutator for multiple attributes.

protected function getAttributeValue($key)
{
    $value = parent::getAttributeValue($key);

    //if the value has already been mutated, don't modify, else if
    //the key is in the array of specified attributes, mutate and return it
    if($value === $this->getAttributeFromArray($key) && in_array($key, $this->attributes_to_mutate) ) {
        $value = $this->myCustomMutator($value);
    }

    return $value;
}

Same goes for setAttributeValue($key, $value)

1
  • Thanks everyone for your valuable comments and suggestions. I am going to use Laravel Accessor. Commented Aug 8, 2018 at 16:55
1

save array to config/your_config.php

<?php

return [

    'columns' => [
        1 => 'string_1',
        2 => 'string_2',
    ],

];

usage:

config('your_config.columns')[1] // string_1
config('your_config.columns')[2] // string_2
1
  • Thank you for your answer. I did exactly the same. Commented Aug 9, 2018 at 2:02

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.