0

I've just started to create an application using the Laravel framework for the first time.

Eloquent seems to be a very powerful and code-saving tool, but I can't figure out how to use different attribute names in the model than in the database table.

Here my conflict:

Database: iddog, dtname, dtbirth, dtfoo, fimom, fidad
Attributes: id, name, birth, foo, mom, dad

Is there a possibility to do that in a model that extends from Eloquent in the Laravel framework? Naming attributes differently than the associated database fields?

Or isn't it cool anymore to call fields in a database table like I do here?

Thank's in advance!

4

2 回答 2

1

现在,在 laravel 5.1 中,我们将其称为Accessor而不是 Getters & Setters。

所以这些天我们有访问器来创建像这样的新属性

public function getPlusFiveAttribute()
{
    return $this->plus_five + 5;
}

不幸的是,如果您已经有一个名为“plus_five”的表字段 - 此访问器将不起作用。

你需要这样,它有效

public function getPlusFiveAttribute()
{
    return $this->attributes['plus_five'] + 5;
}

这是另一种有效的方法:

public function getPlusFiveAttribute($value)
{
    return $value + 5;
}
于 2016-03-02T22:52:03.607 回答
0

为什么数据库的名称与您的模型不同?

虽然你可以做到——你只是为自己创造工作,并引入可能的新错误。只需使用数据库名称。

如果您必须检查Getters & Setters

于 2013-02-24T14:25:07.013 回答