laravel 如何使用驼峰式大小写访问属性?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25559558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 10:01:10  来源:igfitidea点击:

How can I access attributes using camel case?

laraveleloquentnaming-conventions

提问by Lazlo

To be consistent over my coding style, I'd like to use camelCaseto access attributes instead of snake_case. Is this possible in Laravel without modifying the core framework? If so, how?

为了与我的编码风格保持一致,我想使用camelCase访问属性而不是snake_case. 这在 Laravel 中可以不修改核心框架吗?如果是这样,如何?

Example:

例子:

// Database column: first_name

echo $user->first_name; // Default Laravel behavior
echo $user->firstName; // Wanted behavior

回答by Lazlo

Create your own BaseModelclass and override the following methods. Make sure all your other models extendyour BaseModel.

创建您自己的BaseModel类并覆盖以下方法。确保所有其他模型都是extend您的BaseModel.

class BaseModel extends Eloquent {

    // Allow for camelCased attribute access

    public function getAttribute($key)
    {
        return parent::getAttribute(snake_case($key));
    }

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

}

Then for usage:

然后用于使用:

// Database column: first_name

echo $user->first_name; // Still works
echo $user->firstName; // Works too!

This trick revolves around forcing the key to snake case by overriding the magic method used in Model.

这个技巧围绕着通过覆盖Model.

回答by Bouke Versteegh

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

由于 SO 不允许在评论中粘贴代码片段,因此我将此作为新答案发布。

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.

为了确保预先加载不会中断,我不得不修改@Lazlo 的答案。当通过不同的键访问急切加载的关系时,它们会被重新加载。

<?php

class BaseModel extends Eloquent
{

    public function getAttribute($key)
    {
        if (array_key_exists($key, $this->relations)) {
            return parent::getAttribute($key);
        } else {
            return parent::getAttribute(snake_case($key));
        }
    }

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

回答by pwyg

Just thought I'd post this in case it helps anyone else. Though the entry by Boukeis great it does not address lazy-loaded relations that use a camel-case name. When this occurs we simply need to check for the method name in addition to the other checks. The following is what I did:

只是想我会发布这个以防它对其他人有帮助。尽管Bouke的条目很棒,但它没有解决使用驼峰式名称的延迟加载关系。发生这种情况时,除了其他检查外,我们只需要检查方法名称。以下是我所做的:

class BaseModel extends Eloquent
{

    public function getAttribute($key)
    {

        if (array_key_exists($key, $this->relations)
          || method_exists($this, $key)
        )
        {
            return parent::getAttribute($key);
        }
        else
        {
            return parent::getAttribute(snake_case($key));
        }
    }

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