Created
January 13, 2014 09:49
-
-
Save christophrumpel/8397321 to your computer and use it in GitHub Desktop.
Laravel eager loading problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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