方法调用的区别 $model->relation();和 $model->relation; [英] Difference between method calls $model->relation(); and $model->relation;

查看:23
本文介绍了方法调用的区别 $model->relation();和 $model->relation;的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里有一些我缺少的基本理解/理论.我不明白这些函数调用之间的区别:

There is some basic understanding/theory here that I am missing.I don't understand the difference between these function calls:

$distributors = $store->distributors();
$distributors = $store->distributors;
$distributors = $store->distributors()->get();
$distributors = $store->distributors->get();

我在这里想要完成的是获取商店的经销商列表(多对多关系),然后他们将每个经销商的啤酒列表放入一个巨大的列表中.

What I am trying to accomplis here is to get a list of the distributors for a store (a many to many relationship), and they get each distributors list of beers into one giant list.

foreach ($distributors as $distributor) 
{
    $available_beers = array_merge($distributor->beers(), $available_beers);
}

我不知道这是否是最好的方法,而且我无法让它发挥作用.和第一个方法列表类似,不知道是需要->$beers还是->$beers()

I don't know if that is the best way to do this and I can't get it to work. Similar to the first list of methods, I don't know if I need ->$beers or ->$beers()

更新

感谢所有回答的人!这对我今后的工作将是一个很好的参考.我最大的教训是取回集合与取回查询构建器/关系对象之间的区别.为了将来找到这个问题的人的参考,这是我在控制器中设置的内容:

Thanks to everyone who answered! This will be a good reference for me going forward. My biggest lesson was the difference between getting a collection back, vs getting the query builder/relationship object back. For future reference to those who find this question, here is what I set up in my controller:

$store = $this->store->find($id)->first();
$distributors = $store->distributors;
$beers = [];
foreach ($distributors as $distributor){
    $beers = array_merge($distributor->beers->lists('name', 'id'), $beers);
}

推荐答案

简短回答

$model->relation() 返回关系对象

$model->relation 返回关系的结果

$model->relation() 可以解释得非常简单.您正在调用您定义关系的实际函数.你的 distributor 可能看起来有点像这样:

$model->relation() can be explained pretty simple. You're calling the actual function you defined your relation with. Yours for distributor probably looks somewhat like this:

public function distributors(){
    return $this->hasMany('Distributor');
}

所以当调用 $store->distributors() 时,你只会得到 $this->hasMany('Distributor') 的返回值,它是一个实例Illuminate\Database\Eloquent\Relations\HasMany

So when calling $store->distributors() you just get the return value of $this->hasMany('Distributor') which is an instance of Illuminate\Database\Eloquent\Relations\HasMany

你什么时候使用它?

如果您想在运行之前进一步指定查询,通常会调用关系函数.例如添加一个 where 语句:

You usually would call the relationship function if you want to further specify the query before you run it. For example add a where statement:

$distributors = $store->distributors()->where('priority', '>', 4)->get();

当然,您也可以这样做:$store->distributors()->get() 但这与 $store->distributors<的结果相同/代码>.

Of course you can also just do this: $store->distributors()->get() but that has the same result as $store->distributors.

这让我想到了对动态关系属性的解释.

Which brings me to the explanation of the dynamic relationship property.

Laravel 在幕后做了一些事情,允许您直接访问关系的结果作为属性.比如:$model->relation.

Laravel does some things under the hood to allow you to directly access the results of a relationship as property. Like: $model->relation.

这是Illuminate\Database\Eloquent\Model

1) 这些属性实际上并不存在.因此,如果您访问 $store->distributors,调用将被代理到 __get()

1) The properties don't actually exist. So if you access $store->distributors the call will be proxied to __get()

2) 然后此方法调用带有属性名称 getAttribute('distributors')

2) This method then calls getAttribute with the property name getAttribute('distributors')

public function __get($key)
{
    return $this->getAttribute($key);
}

3)getAttribute 中,它检查关系是否已经加载(存在于 relations 中).如果没有,并且存在关系方法,它将加载关系 (getRelationshipFromMethod)

3) In getAttribute it checks if the relationship is already loaded (exists in relations). If not and if a relationship method exists it will load the relation (getRelationshipFromMethod)

public function getAttribute($key)
{
    // code omitted for brevity

    if (array_key_exists($key, $this->relations))
    {
        return $this->relations[$key];
    }

    $camelKey = camel_case($key);

    if (method_exists($this, $camelKey))
    {
        return $this->getRelationshipFromMethod($key, $camelKey);
    }
}

4) 最后,Laravel 对关系调用 getResults(),然后在查询构建器实例上产生 get().(这与 $model->relation()->get() 的结果相同.

4) In the end Laravel calls getResults() on the relation which then results in a get() on the query builder instance. (And that gives the same result as $model->relation()->get().

这篇关于方法调用的区别 $model->relation();和 $model->relation;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆