I started using the eager loader in Laravel 4.
I have two tables (campaigns and reports) whit following relation in the Campaigns Model:
public function reports() {
return $this->hasMany('Reports', 'account_id', 'adwords_id')->select('CLICKS', 'COST');
}
I had to use non-conventional column names here unfortunaly.
When I now do:
$data['campaigns'] = Campaign::get();
foreach ($data['campaigns'] as $campaign) {
print_r($campaign->reports);
}
it outputs all the reports for the campaigns the way it should.
But now I want to eager load this:
$data['campaigns'] = Campaign::with('reports')->get();
foreach ($data['campaigns'] as $campaign) {
print_r($campaign->reports);
}
This outputs me just empty arrays.
What am I doing wrong? I really want to use eager to have fasters scripts...
collectionthen it wouldn't work as well, however when you do it on a single parent, then there is no need for matchingparents - children, so basically whatever comes from the query is set as a relation on the parent model.