How can I access attributes using camel case?

后端 未结 3 1793
面向向阳花
面向向阳花 2020-12-14 02:01

To be consistent over my coding style, I\'d like to use camelCase to access attributes instead of snake_case. Is this possible in Laravel without m

3条回答
  •  时光取名叫无心
    2020-12-14 02:47

    Since SO doesn't allow pasting code snippets in comments, I'm posting this as a new answer.

    To make sure that eager loading does not break, I had to modify @Lazlo's answer. When accessing eagerly loaded relations by a different key, they are reloaded.

    relations)) {
                return parent::getAttribute($key);
            } else {
                return parent::getAttribute(snake_case($key));
            }
        }
    
        public function setAttribute($key, $value)
        {
            return parent::setAttribute(snake_case($key), $value);
        }
    }
    

提交回复
热议问题