方法之间的区别调用$建模>关系();和$建模>关系; [英] Difference between method calls $model->relation(); and $model->relation;
问题描述
有一些基本的了解/这里的理论,我missing.I不懂这些功能之间的差别呼叫:
$分销= $ store->经销商();
$分销= $ store->分销商;
$分销= $ store->经销商() - GT;获得();
$分销= $ store->分销商─>获得();
什么我想在这里既成事实是得到分销商的列表商店(一个多对多的关系),他们得到的啤酒经销商每一个清单到一个巨大的名单。
的foreach($经销商为$总代理)
{
$ available_beers = array_merge($分销商─>啤酒(),$ available_beers);
}
我不知道这是否是做最好的办法,我无法得到它的工作。到方法的第一个列表一样,我不知道如果我需要 - > $啤酒
或 - > $啤酒()
更新
感谢大家谁回答!这将是对我来说是很好的参考前进。我最大的教训是得到一个集合回来,让VS查询生成器/关系对象返回之间的不同。对于未来的参考那些谁找到这个问题的,这里是我在我的控制器设置:
$商店= $这个 - > store->找到($ ID) - >首先();
$分销= $ store->分销商;
$啤酒= [];
的foreach($经销商为$总代理){
$啤酒= array_merge($分销商─> beers->清单('名','ID'),$啤酒);
}
简短的回答
$建模>关系()
返回关系对象
$建模>关系
返回结果的关系
龙答案
$建模>关系()
可以解释pretty简单。你叫你定义了关系的实际功能。为分销商你
可能看起来有点像这样的:
公共职能经销商(){
返回$这个 - >的hasMany('经销商');
}
打电话时那么 $ store->经销商()
您刚刚得到的返回值 $这个 - >的hasMany('经销商' )
这是一个实例照亮\\数据库\\锋\\关系\\的hasMany
的当你使用它?的
您如果想在运行之前进一步指定查询通常会调用该函数关系。例如添加一个WHERE语句:
$分销= $ store->经销商() - 化合物其中('优先','>',4) - GT;获得();
当然,你也可以只做到这一点: $ store->经销商() - GT;得到()
但有相同的结果为 $ store-方式>分销
这使我在动态关系属性的解释
Laravel做引擎盖下一些事情,让你直接访问作为财产有关系的结果。爱: $建模>关系
下面是会发生什么照亮\\数据库\\锋\\型号
1)实际上并不存在的属性。所以,如果你访问 $ store->经销商
的通话将被代理到 __的get()
2)这个方法再调用的getAttribute
通过属性名的getAttribute('经销商')
公共职能__get($键)
{
返回$这个 - >的getAttribute($键);
}
3)在的getAttribute
它检查的关系已经被加载(存在于关系
)。如果没有,如果有关系的方法存在,它将负载的关系( getRelationshipFromMethod
)
公共职能的getAttribute($键)
{
// code不再赘述 如果(array_key_exists($键,$这个 - >的关系))
{
返回$这个 - >关系[$关键];
} $ camelKey = camel_case($键); 如果(method_exists($此,$ camelKey))
{
返回$这个 - > getRelationshipFromMethod($键,$ camelKey);
}
}
4)到底Laravel调用 getResults()
上的关系,这则是一个的get()
上查询生成器实例。 (这给出了相同的结果 $建模>的关系() - 方式>得到()
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);
}
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()
Update
Thanks to everyone who answered! This will be a good reference for me going forward. My biggest lesson was the different 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);
}
Short answer
$model->relation()
returns the relationship object
$model->relation
returns the result of the relationship
Long answer
$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');
}
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
When do you use it?
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();
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 does some things under the hood to allow you to directly access the results of a relationship as property. Like: $model->relation
.
Here's what happens in Illuminate\Database\Eloquent\Model
1) The properties don't actually exist. So if you access $store->distributors
the call will be proxied to __get()
2) This method then calls getAttribute
with the property name getAttribute('distributors')
public function __get($key)
{
return $this->getAttribute($key);
}
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) 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()
.
这篇关于方法之间的区别调用$建模>关系();和$建模>关系;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!