我有以下查询返回一个对象,然后将该对象发送到视图
$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',
这里是一个为目标返回的内容示例,其中包含具有异常值的强度目标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被转换为蛇案?无论我做了什么(数据库,ralhiphip方法等),我都使用了蛇案。
现在我的主要问题
我可以通过
访问目标@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-> excersise只返回1和$ goal-> strengthGoalDesc-> excerises-> name gives
ErrorException
Trying to get property of non-object (View: /var/www/app/views/users/show.blade.php)
尝试访问它就像一个数组$ goal-> strengthGoalDesc ['excersises']只返回1
编辑:我试图访问名称属性的代码
@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($ goal-&gt; strengthGoalDesc)输出
null
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($ goal-&gt; stengthGoalDesc ['excersise'] *
null
string '1' (length=1)
null
答案 0 :(得分:2)
注意null
给出的var_dump()
输出。第一次和第三次尝试访问$goal->stengthGoalDesc->excersise
时。 $goal->stengthGoalDesc
变量为null
,导致 ErrorException 。
尝试将其包装在 if语句中,以检查$goal->strengthGoalDesc
是否为空:
if( $goal->strengthGoalDesc !== null ) {
var_dump( $goal->strengthGoalDesc->exercise );
}
那应该解决ErrorException。希望它有所帮助。
答案 1 :(得分:1)
我猜这是一个结构问题(所有数据看起来都不错,但你在一个糟糕的时刻称之为)
@foreach($user->goals as $goal)
{{ $goal }}
$goal->strengthGoalDesc->excersise
//this will throw an error because not all Goals have strengthGoalDesc
@endforeach
请查看您的foreach或粘贴更多代码。 (您尝试使用strengthGoalDesc的地方)
答案 2 :(得分:0)
您的问题是您有重叠的属性和关系名称。您有一个名为exercise
的数据库列以及名称相同的关系,并且属性优先于关系。将列重命名为exerciseId
或其他内容。
至于转换为snake_case,请尝试在模型上设置public static $snakeAttributes = false
或所有模型扩展的基础模型。