doudi2229 2014-01-16 18:02
浏览 24
已采纳

访问懒惰的热切加载属性

I have the following query returning an object which is then sent to the view

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

In the view I am doing a number of things to display all this information and it was all going great until I tried to access the properties related to these relationships

            'goals.strengthGoalDesc.excersise',
            'goals.distanceGoalDesc.excersise',

here is an example of what is being returned for a goal with a strength goal desc that has an excersise

{
"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} 

my first question is why are bodyGoalDesc, distanceGoalDesc and strengthGoalDesc being converted to snake case? Nowhere in what I have done(the database, raltionship methods etc) have I used snake case.

Now my main problem

I can access the goal with

@foreach($user->goals as $goal)
    {{ $goal }}
@endforeach

and $goal->strengthGoalDesc returns

{"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"}

but $goal->strengthGoalDesc->excersise just returns 1 and $goal->strengthGoalDesc->excerises->name gives

ErrorException
Trying to get property of non-object (View: /var/www/app/views/users/show.blade.php)

and trying to access it like an array $goal->strengthGoalDesc['excersises'] just returns 1

edit: code where I am trying to access the name property

@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>

and I thought it might be because it is a collection but wrapping it in a foreach only returned an invalid argument exception as it says it is not an object

I am sure I am jsut doing something stupid but this has me totally lost

var_dump($goal->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

and *var_dump($goal->stengthGoalDesc['excersise']*

null
string '1' (length=1)
null
  • 写回答

3条回答 默认 最新

  • dqdl6469 2014-01-20 15:48
    关注

    I guess this a structure problem (all the data looks good but you call it in a bad moment)

    @foreach($user->goals as $goal)
        {{ $goal }}
        $goal->strengthGoalDesc->excersise 
        //this will throw an error because not all Goals have strengthGoalDesc
    @endforeach
    

    Please look in your foreach's or paste some more code. (where you try to use strengthGoalDesc)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号