laravel/framework — pull request #5695 by tremby (2014-09-09 18:19:04+00:00)
Title: Add failing test: hasMany and null foreign key

This relates to https://github.com/laravel/framework/issues/5649

If Foo hasMany Bar, and a Bar exists with its foo_id set to null (i.e.
it does not belong to any Foo), Laravel will currently find that Bar
when looking for Bars related to a fresh unsaved Foo (which still has
an id of null), since the id and foreign key match. Null should never
be a foreign key match.

class Foo extends Eloquent {
public function bars() {
return $this->hasMany('Bar');
}
}
class Bar extends Eloquent {}
$bar = Bar::create(); // Saved but no foo_id
$foo = new Foo(); // Unsaved, still has id of null
echo count($foo->bars); // Gives 1 (the $bar above), should give 0

Disclaimer: I am not certain I have written the test correctly.