我有以下查询返回一个对象,然后将其发送到视图访问懒洋洋地急于加载性能
$user = $this->user->find($id)
->load(
'images',
'albums',
'measurements',
'medicalAnswers.medicalQuestion',
'goals.strengthGoalDesc.excersise',
'goals.distanceGoalDesc.excersise',
'goals.bodyGoalDesc',
'client.trainers.user',
'trainer.clients.user',
'trainer.classes',
'trainer.types',
'trainer.programs.weeks.days.sessions.phases.excersises',
'trainer.testimonials'
);
在我做了一些事情,以显示所有这些信息的视图,它是所有打算伟大的,直到我试图访问与这些关系
'goals.strengthGoalDesc.excersise',
'goals.distanceGoalDesc.excersise',
这里的属性是返回什么是目标与具有excersise强度目标desc的例子
{
"id":"2",
"userId":"1",
"title":"strength goal title",
"goalDesc":"This should describe my strngth goal in text form",
"goalStatus":"0",
"bodyGoalId":null,
"strengthGoalId":"1",
"distanceGoalId":null,
"created_at":"2014-01-16 15:51:16",
"updated_at":"2014-01-16 15:51:16",
"strength_goal_desc":{
"id":"1",
"excersise":{
"id":"1",
"name":"Leg Extension",
"type":"Resistance",
"variation":null,
"equipment":"Machine",
"focus":"Legs",
"desc":"",
"video":null,
"created_at":"2014-01-16 15:51:18",
"updated_at":"2014-01-16 15:51:18"},
"sets":"4",
"reps":"10",
"weight":"70.00",
"created_at":"2014-01-16 15:51:16",
"updated_at":"2014-01-16 15:51:16"},
"distance_goal_desc":null,
"body_goal_desc":null}
我的第一个问题是为什么bodyGoalDesc,distanceGoalDesc和strengthGoalDesc被转换为蛇情况?在我所做的事情(数据库,联系方式等)方面,我没有使用蛇的情况。现在
我的主要问题
我可以
@foreach($user->goals as $goal)
{{ $goal }}
@endforeach
,并访问目标$目标 - > strengthGoalDesc返回
{"id":"1","excersise":{"id":"1","name":"Leg Extension","type":"Resistance","variation":null,"equipment":"Machine","focus":"Legs","desc":"","video":null,"created_at":"2014-01-16 15:51:18","updated_at":"2014-01-16 15:51:18"},"sets":"4","reps":"10","weight":"70.00","created_at":"2014-01-16 15:51:16","updated_at":"2014-01-16 15:51:16"}
但$目标 - > strengthGoalDesc-> excersise只是返回1 and $ goal-> strengthGoalDesc-> excerises-> name给出
ErrorException
Trying to get property of non-object (View: /var/www/app/views/users/show.blade.php)
,并试图访问它像一个数组$目标 - > strengthGoalDesc [“excersises”]刚刚返回1
编辑:代码在那里我试图访问的name属性
@elseif($goal->strengthGoalId !== null)
<th>{{ 'Strength' }}</th>
<th>
Excersise: {{ $goal->strengthGoalDesc->excersise->name) }}
Weight: {{ $goal->strengthGoalDesc->weight }}</br>
Sets: {{ $goal->strengthGoalDesc->sets }}</br>
Reps: {{ $goal->strengthGoalDesc->reps }}</br>
</th>
,我想这可能是因为它是一个集合,但它包裹在foreach只是因为它说,它是不是一个对象
我相信我仅仅指刚做一些愚蠢的事返回无效参数异常,但是这有我完全失去了
的var_dump($目标 - > strengthGoalDesc)
OUPUTSnull
object(StrengthGoalDesc)[506]
protected 'guarded' =>
array (size=0)
empty
protected 'table' => string 'strengthGoalDescs' (length=17)
protected 'connection' => null
protected 'primaryKey' => string 'id' (length=2)
protected 'perPage' => int 15
public 'incrementing' => boolean true
public 'timestamps' => boolean true
protected 'attributes' =>
array (size=7)
'id' => string '1' (length=1)
'excersise' => string '1' (length=1)
'sets' => string '4' (length=1)
'reps' => string '10' (length=2)
'weight' => string '70.00' (length=5)
'created_at' => string '2014-01-16 15:51:16' (length=19)
'updated_at' => string '2014-01-16 15:51:16' (length=19)
protected 'original' =>
array (size=7)
'id' => string '1' (length=1)
'excersise' => string '1' (length=1)
'sets' => string '4' (length=1)
'reps' => string '10' (length=2)
'weight' => string '70.00' (length=5)
'created_at' => string '2014-01-16 15:51:16' (length=19)
'updated_at' => string '2014-01-16 15:51:16' (length=19)
protected 'relations' =>
array (size=1)
'excersise' =>
object(Excersise)[480]
protected 'guarded' =>
array (size=0)
...
protected 'table' => string 'excersises' (length=10)
protected 'connection' => null
protected 'primaryKey' => string 'id' (length=2)
protected 'perPage' => int 15
public 'incrementing' => boolean true
public 'timestamps' => boolean true
protected 'attributes' =>
array (size=10)
...
protected 'original' =>
array (size=10)
...
protected 'relations' =>
array (size=0)
...
protected 'hidden' =>
array (size=0)
...
protected 'visible' =>
array (size=0)
...
protected 'appends' =>
array (size=0)
...
protected 'fillable' =>
array (size=0)
...
protected 'dates' =>
array (size=0)
...
protected 'touches' =>
array (size=0)
...
protected 'observables' =>
array (size=0)
...
protected 'with' =>
array (size=0)
...
public 'exists' => boolean true
protected 'softDelete' => boolean false
protected 'hidden' =>
array (size=0)
empty
protected 'visible' =>
array (size=0)
empty
protected 'appends' =>
array (size=0)
empty
protected 'fillable' =>
array (size=0)
empty
protected 'dates' =>
array (size=0)
empty
protected 'touches' =>
array (size=0)
empty
protected 'observables' =>
array (size=0)
empty
protected 'with' =>
array (size=0)
empty
public 'exists' => boolean true
protected 'softDelete' => boolean false
null
和*的var_dump($目标 - > stengthGoalDesc [ 'excersise'] *
null
string '1' (length=1)
null
做一个'var_dump'来查看它是什么类型的对象。它可能是一个数组,而不是一个Std对象。尝试'$ goal-> strengthGoalDesc ['excersise']' – elclanrs
感谢您的快速回复$ goal-> strengthGoalDesc ['excersise']只是返回'1',我已经添加了var_dump的输出到我的问题,它看起来像一个对象给我 – Ir1sh