2014-01-16 54 views
2

我有以下查询返回一个对象,然后将其发送到视图访问懒洋洋地急于加载性能

$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)

OUPUTS
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($目标 - > stengthGoalDesc [ 'excersise'] *

null 
string '1' (length=1) 
null 
+0

做一个'var_dump'来查看它是什么类型的对象。它可能是一个数组,而不是一个Std对象。尝试'$ goal-> strengthGoalDesc ['excersise']' – elclanrs

+0

感谢您的快速回复$ goal-> strengthGoalDesc ['excersise']只是返回'1',我已经添加了var_dump的输出到我的问题,它看起来像一个对象给我 – Ir1sh

回答

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

通知的null输出该var_dump()给出。第一次和第三次当您尝试访问$goal->stengthGoalDesc->excersise$goal->stengthGoalDesc变量是null,这导致ErrorException

尝试在包装,如果声明检查$goal->strengthGoalDesc不为空:

if($goal->strengthGoalDesc !== null) { 
    var_dump($goal->strengthGoalDesc->exercise); 
} 

这应该解决ErrorException。希望能帮助到你。

+0

谢谢你的回复。这里我特别的问题是它不应该为空,因为数据库中有关于该特定关系的信息。信息由$ goal-> strengthGoalDesc返回(请参阅第5个代码块输出),但由于某种原因我无法单独访问 – Ir1sh

+0

@ Ir1s​​h我明白了。所以问题似乎在于模型之间的关系绑定,对吗? 尝试使用'DB :: getQueryLog()'转储原始查询。也许你会在那里找到答案 - 因为ORM并不总是按预期工作。 – mauvm

+0

对不起,我应该补充说,它被包装在一个if语句中 – Ir1sh

0

你的问题是你有重叠的属性和关系名称。您有一个名为exercise的数据库列以及一个名称相同的关系,并且属性优先于关系。将该列重命名为exerciseId或其他。

至于转换为snake_case,请尝试在您的模型或基础模型上设置public static $snakeAttributes = false,其中所有模型都从中延伸。

相关问题