Php 访问延迟加载的属性
下面的查询返回一个对象,然后将该对象发送到视图Php 访问延迟加载的属性,php,laravel-4,Php,Laravel 4,下面的查询返回一个对象,然后将该对象发送到视图 $user = $this->user->find($id) ->load( 'images', 'albums', 'measurements', 'medicalAnswers.medicalQuestion', 'goals.strengthG
$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',
下面是一个示例,它说明了一个目标返回的内容,该目标具有一个练习的strength goal 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被转换为snake case?在我所做的工作中(数据库、关系方法等),我没有使用过snake case
现在我的主要问题是
我可以用电脑进入目标
@foreach($user->goals as $goal)
{{ $goal }}
@endforeach
和$goal->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"}
但是$goal->strengthGoalDesc->exercise只返回1,而$goal->strengthGoalDesc->execises->name只返回1
ErrorException
Trying to get property of non-object (View: /var/www/app/views/users/show.blade.php)
尝试像数组$goal->strengthGoalDesc['exercises']一样访问它只返回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>
和*var_dump($goal->Strengthgoaldesc['exersise']*
null
string '1' (length=1)
null
注意
var_dump()
给出的null
输出。第一次和第三次尝试访问$goal->stengtgoaldesc->exersise
时,$goal->stengtgoaldesc
变量是null
,这会导致错误异常
尝试将其包装在if语句中,检查$goal->strengthGoalDesc
是否为空:
if( $goal->strengthGoalDesc !== null ) {
var_dump( $goal->strengthGoalDesc->exercise );
}
这应该可以解决ErrorException。希望它能有所帮助。我想这是一个结构问题(所有的数据看起来都不错,但你在一个糟糕的时刻调用它)
请查看您的foreach或粘贴更多代码。(在您尝试使用strengthGoalDesc的地方)您的问题是属性和关系名称重叠。您有一个名为
exercise
的数据库列以及一个名为相同的关系,属性优先于关系。请将该列重命名为exerciseId
或其他名称
至于转换为snake_的情况,请尝试在您的模型或所有模型扩展的基础模型上设置
public static$snakeAttributes=false
。执行var_dump
以查看它是什么类型的对象。它可能是数组,而不是标准对象。尝试$goal->strengthGoalDesc['excersise']
感谢您的快速回复$goal->strengthGoalDesc['excersise']简单地返回“1”,我已将var_dump的输出添加到我的问题中。它看起来像是一个对象,用于向您发送回复。我这里的特殊问题是,它不应为null,因为数据库中有该特定关系的信息。该信息由$goal->strengthGoalDesc返回(请参阅第5个代码块了解输出)但我无法单独访问某些代码块reason@Ir1sh我明白了。所以问题似乎在于模型之间的关系绑定,对吗?尝试使用DB::getQueryLog()转储原始查询
。也许你会在那里找到答案-因为ORM并不总是按预期工作。抱歉,我应该补充一点,它被包装在if语句中原始查询是否让你走得更远?试着自己在数据库中运行它们,大多数情况下这有助于你更好地了解幕后发生的事情。
if( $goal->strengthGoalDesc !== null ) {
var_dump( $goal->strengthGoalDesc->exercise );
}
@foreach($user->goals as $goal)
{{ $goal }}
$goal->strengthGoalDesc->excersise
//this will throw an error because not all Goals have strengthGoalDesc
@endforeach