My Child Class
class Child extends Eloquent { ... public function campaign() { return $this->hasOne('Campaign'); } ... }
Here is my 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 as $child), I try to call echo $child->campaign->name and I got "Trying to get property of non-object" message. $child->name works fine!