Skip to content

Instantly share code, notes, and snippets.

@christophrumpel
Created January 13, 2014 09:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christophrumpel/8397321 to your computer and use it in GitHub Desktop.
Save christophrumpel/8397321 to your computer and use it in GitHub Desktop.
Laravel eager loading problem
// This is working
$fanpages = Fanpage::all();
// Loop through fanpages
foreach($fanpages as $fanpage) {
// Check if there are entries for yesterday (data date)
echo $fanpage->name;
return $fanpage->fanpageActivities()->get();
}
// And this is working
return Project::with('fanpages')->get();
// But not his: This leaves me with empty result
return Fanpage::with('fanpageActivities')->get();
// Model
<?php
class Fanpage extends Eloquent {
// Protect for mass assignment
// We need to specifiy the protected oder fillable arguments
protected $fillable = array('project_id', 'facebook_id', 'name', 'username', 'color');
// Set data start date for filling new fanpages with empty data
public static $dataStartDate = '2013-06-18';
public function fanpageActivities() {
return $this->hasMany('FanpageActivity');
}
public function projects() {
return $this->belongsToMany('Project');
}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment