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