Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel DB first() "Trying to get property of non-object"

I run a query using Laravel's DB first() which returns an object, when I check using dd() or vardump(). But when I try to print the value using echo ($promotion->pp_name); it gives error, but same property shows while dd($promotion->pp_name);

<?php dd($promotion->pp_name); ?> prints "urgent"

<?php echo ($promotion->pp_name); ?> but it gives "Trying to get property of non-object"

Full object dump results: <?php dd($promotion); ?>

    {#196 ▼
  +"ppo_id": 23
  +"ppo_prj_id": 68
  +"ppo_pp_id": 4
  +"ppo_updated_date": "2014-05-20"
  +"ppo_status": 1
  +"pp_id": 4
  +"pp_name": "urgent"
  +"pp_dispText": "I want my project to be marked as an urgent project"
  +"pp_amount": "5.00"
  +"pp_updated_date": "2013-08-09"
  +"pp_status": 1
}

and the function that return this object.

function getProjectPromotion($value='')
{
    $project_id = $value; 

    $promotion =     DB::table('project_promotion_option')
                    ->join('project_promotion', 'project_promotion_option.ppo_pp_id', '=', 'project_promotion.pp_id')
                    ->where('ppo_prj_id',  '=' ,  $project_id )
                    ->first(); 

    return $promotion; 

}
like image 710
Shahid Chaudhary Avatar asked May 13 '16 06:05

Shahid Chaudhary


1 Answers

Are you calling this method in a loop? When you do a dd(), the script stops with the right result after the first loop run and all is good. But when you do echo in a loop, it continues and my guess is that at some point you pass some corrupted data that breaks the method.

We would need to see the code excerpt where you are calling the mentioned method to verify this hunch.

like image 84
Tadas Paplauskas Avatar answered Oct 18 '22 01:10

Tadas Paplauskas