I’ve been trying to read an object for 3 hours without success.
I want to get the id inside this object.
with print_r($meal)
:
AppModelsRecipe Object ( [connection:protected] => mysql [table:protected] => recipes [primaryKey:protected] => id [keyType:protected] => int [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [preventsLazyLoading] => [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [escapeWhenCastingToString:protected] => [attributes:protected] => Array ( [id] => 29 [recipe] => Creamy Cheese Pancakes [category] => Breakfast Recipes [meal] => breakfast [image] => Creamy_Cheese_Pancakes-Creamy_Cheese_Pancakes.jpg [calories] => 82 [created_at] => [updated_at] => ) [original:protected] => Array ( [id] => 29 [recipe] => Creamy Cheese Pancakes [category] => Breakfast Recipes [meal] => breakfast [image] => Creamy_Cheese_Pancakes-Creamy_Cheese_Pancakes.jpg [calories] => 82 [created_at] => [updated_at] => ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [classCastCache:protected] => Array ( ) [attributeCastCache:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [timestamps] => 1 [hidden:protected] => Array ( ) [visible:protected] => Array ( ) [fillable:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) )
If I do print_r($meal->id)
or print_r($meal['id'])
I have the id:
29
But if I do $id = $meal['id]
, i get this:
Undefined array key "id"
If I do $id = $meal->id
:
Attempt to read property "id" on array
I don’t understand why.
I tried:
$id = $meal[0]['id'];
Also tried:
$json_decode = json_decode($meal, true);
$id = $json_decode['id'];
What am I doing wrong?
Thank you