I am unable to locate the problem in the following example, I have been looking at the http://laravel.com/docs/eloquent#inserting-related-models but for some reason which I am unable to locate, it's not working.
Models:
class Article extends Eloquent {
public function category()
{
$this->belongsTo('Category');
}
}
class Category extends Eloquent {
public function articles()
{
$this->hasMany('Article');
}
}
DB schema:
Articles has a column called category_id which is an integer, and both Articles and Categories have increments on ID column.
Unit test which is currently failing:
public function testCreateArticleWithCategory()
{
$category = new Category();
$category->name = 'Kitties';
$this->assertTrue($category->save());
$cid = $category->id;
$category = Category::find($cid);
$article = new Article();
$article->title = 'Hello Kitty';
$article = $category->articles()->save($article);
$this->assertEquals('Kitties', $article->category->name);
}
The test is failing with the following message:
Starting test 'ArticleTest::testCreateArticleWithCategory'. PHP Fatal error: Call to a member function save() on a non-object in /Users/kristiannissen/Documents/php/sacrebleu/app/tests/ArticleTest.php on line 30
And line 30 is
$article = $category->articles()->save($article);