【发布时间】:2015-10-13 08:56:09
【问题描述】:
我有 3 个模型:用户、A、B 和 C。
用户.php
public function a()
{
return $this->belongsTo('App\A');
}
A.php
public function b(){
return $this->belongsTo('App\B');
}
B.php
public function cRelation(){
return $this->hasOne('App\C');
}
然后,我执行我的查询并加载关系
$tests = User::all();
$tests->load('a.b.cRelation');
现在,在我的视图文件中,如果我打印这个:
@foreach($tests as $test)
{{$test->a->b}}
@endforeach
我可以按预期看到我的 c_relation 魔法属性。 但是,如果我尝试访问它,则不会打印任何内容。
我哪里错了?为什么如果我打印父对象($test->a->b),我可以看到属性,但我不能打印?
【问题讨论】:
-
您是如何尝试访问该属性的?
$test->a->b->cRelation? -
{{$test->a->b->c_relation}} 因为在打印时 {{$test->a->b}} 我看到了 c_relation 属性,但现在,我试图使用 {{$test->a->b->cRelation}} 访问它并且它有效。所以,这个属性是不真实的。谢谢
标签: php laravel eloquent relationships