Saturday 15 February 2014

php - Laravel query join gives Trying to get property of non-object -



php - Laravel query join gives Trying to get property of non-object -

my kid class

class kid extends eloquent { ... public function campaign() { homecoming $this->hasone('campaign'); } ... }

here query in laravel:

$query = child::query(); $query->select('children.*'); $query->join('demands', 'demands.children_id', '=', 'children.id')->where('demands.accepted', '=', '1'); $children = $query->orderby(db::raw('rand()'))->take(4)->get();

in in foreach ($children $child), seek phone call echo $child->campaign->name , got "trying property of non-object" message. $child->name works fine!

so turned out, not every children had campaign associated it. add together little check in foreach loop , should fine

foreach($children $child){ if($child->campaign == null){ continue; } // stuff }

php laravel laravel-4

No comments:

Post a Comment