php Laravel 获取模型属性:两种方法的区别

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26047536/
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-08-25 18:07:15  来源:igfitidea点击:

Laravel get model attribute: difference between two methods

phplaravelmodelattributeseloquent

提问by JasonK

I found out that there are two ways to get/display a model's attribute with Laravel. I could create a function in User.phpfor example:

我发现有两种方法可以使用 Laravel 获取/显示模型的属性。例如,我可以创建一个函数User.php

public function getUsername() {
    return $this->username;
}

Then display the username like this:

然后像这样显示用户名:

{{{ Auth::user()->getUsername() }}}

Or I could just simply do this without needing to create a function:

或者我可以简单地执行此操作而无需创建函数:

{{{ Auth::user()->username }}}

What is the difference between these two methods?

这两种方法有什么区别?

采纳答案by Marcin Nabia?ek

There is no difference. You get exactly the same result in this case so this function won't be too helpful in this case. In above case you simply created function that returns one of field, but you could also create function that return 2 fields separated by comma:

没有区别。在这种情况下,您会得到完全相同的结果,因此此功能在这种情况下不会有太大帮助。在上述情况下,您只是创建了返回字段之一的函数,但您也可以创建返回由逗号分隔的 2 个字段的函数:

public function getUserAge() {
    return $this->username.', '.$this->age;
}

and then you could use

然后你可以使用

{{{ Auth::user()->getUserAge() }}}

in your view.

在你看来。

But you can also create accessor and mutatorsfor your model attributes to change what you set/get from your model

但是您也可以为您的模型属性创建访问器和修改器,以更改您从模型中设置/获取的内容

回答by The Alpha

When Using $someObject->usernamein this case the __get()magic method returns the property from the attributesarray because the usernameproperty is not a public property of the object but it's stored (after populating) inside the attributesarray and when you are calling the getUserName()custom method, the method is returning the property and indirectly the same thing is happening. So, you don't need to use a method for this.

$someObject->username在这种情况下使用时,__get()魔术方法从attributes数组中返回属性,因为该username属性不是对象的公共属性,但它存储(填充后)在attributes数组中,并且当您调用getUserName()自定义方法时,该方法正在返回该属性间接地,同样的事情正在发生。因此,您不需要为此使用方法。

In both cases, the __get()magic method is being triggered to return the item from the attributes array. Check about Magic Methodson PHPmanual.

在这两种情况下,__get()都会触发魔法方法以从属性数组中返回项目。检查手册上的魔术方法PHP

In some case, you may need to use a dynamic non-existingproperty, for example, if you have a first_nameand last_namefield/property and you want to use both names together as a full_nameproperty then in this case, Laravelprovides a nice way to get it. All you need to do is that, create a method like:

在某些情况下,您可能需要使用动态non-existing属性,例如,如果您有一个first_namelast_name字段/属性,并且您想将两个名称一起用作一个full_name属性,那么在这种情况下,Laravel提供了一个很好的方法来获取它。您需要做的就是创建一个方法,例如:

public function getFullNameAttribute()
{
    return $this->first_name . ' ' . $this->last_name;
}

So then, you may use {{ $user->full_name }}to get that concatenated value.

那么,您可以使用{{ $user->full_name }}来获取该串联值。

回答by Jeff Lambert

Pretty much no difference, except adding a wrapper function for getting it via just by the attribute will add the overhead of an extra function call, which you don't need (thus minutely decreasing performance).

几乎没有区别,除了添加一个包装函数以通过属性获取它会增加额外函数调用的开销,而您不需要它(因此会细微地降低性能)。

Getting an attribute (in your example by doing just $this->username) will ultimately boil down to calling this function in Illuminate\Database\Eloquent\Model:

获取属性(在您的示例中通过执行 just $this->username)最终将归结为在 中调用此函数Illuminate\Database\Eloquent\Model

/**
 * Dynamically retrieve attributes on the model.
 *
 * @param  string  $key
 * @return mixed
 */
public function __get($key)
{
    return $this->getAttribute($key);
}

Wrapping this function call with your own is quite unnecessary.

用你自己的函数调用来包装这个函数调用是完全没有必要的。

回答by Alley Shairu

No difference one is propertyor fieldof the class while other is a getter method which also returns that field. It is just common example of OOP.

没有区别,一个是类的属性字段,而另一个是 getter 方法,它也返回该字段。这只是 OOP 的常见示例。

Since it a model object you can call all of that model methods and property if they are public and protected.

由于它是一个模型对象,如果它们是公共和受保护的,您可以调用所有这些模型方法和属性。