{"action":"created","issue":{"url":"https://api.github.com/repos/laravel/framework/issues/5289","labels_url":"https://api.github.com/repos/laravel/framework/issues/5289/labels{/name}","comments_url":"https://api.github.com/repos/laravel/framework/issues/5289/comments","events_url":"https://api.github.com/repos/laravel/framework/issues/5289/events","html_url":"https://github.com/laravel/framework/issues/5289","id":39211173,"number":5289,"title":"Prevent lazy load hasMany optional relation when foreign_key is null","user":{"login":"dvlpp","id":973325,"avatar_url":"https://avatars.githubusercontent.com/u/973325?v=3","gravatar_id":"","url":"https://api.github.com/users/dvlpp","html_url":"https://github.com/dvlpp","followers_url":"https://api.github.com/users/dvlpp/followers","following_url":"https://api.github.com/users/dvlpp/following{/other_user}","gists_url":"https://api.github.com/users/dvlpp/gists{/gist_id}","starred_url":"https://api.github.com/users/dvlpp/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dvlpp/subscriptions","organizations_url":"https://api.github.com/users/dvlpp/orgs","repos_url":"https://api.github.com/users/dvlpp/repos","events_url":"https://api.github.com/users/dvlpp/events{/privacy}","received_events_url":"https://api.github.com/users/dvlpp/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/laravel/framework/labels/bug","name":"bug","color":"fc2929"},{"url":"https://api.github.com/repos/laravel/framework/labels/database","name":"database","color":"eb6420"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2014-07-31T16:18:29Z","updated_at":"2015-05-12T21:45:06Z","closed_at":null,"body":"I've got a hasMany relationship defined this way, in a <code>Command</code> Eloquent model:\r\n\r\n    public function rows()\r\n    {\r\n        return $this->hasMany('\\Acme\\Models\\Row')\r\n    }\r\n\r\nIn my case, rows can be linked to Commands, or not. So my Row migration file contains a nullable foreign key:\r\n\r\n    $table->integer('command_id')->unsigned()->nullable()->index();\r\n    $table->foreign('command_id')->references('id')->on('commands')->onDelete('set null');\r\n\r\nI realize thought that if I instantiate a new Command object and call <code>$command->rows</code>, instead of an empty Collection, I get all the rows with a null command_id.\r\n\r\nThis is perfectly explained by the SQL generated by this <code>$command->rows</code> call:\r\n\r\n    select * from `rows` where `rows`.`command_id` is null\r\n\r\nI think it's strange that Laravel choose to consider valid a null foreign key... What do you think: is this a bug? Am I missing something? What could be the best way to prevent this behavior?\r\n\r\n(pasted from http://stackoverflow.com/questions/25063201/laravel-4-prevent-lazy-load-hasmany-relation-when-foreign-key-is-null-optional)"},"comment":{"url":"https://api.github.com/repos/laravel/framework/issues/comments/101430651","html_url":"https://github.com/laravel/framework/issues/5289#issuecomment-101430651","issue_url":"https://api.github.com/repos/laravel/framework/issues/5289","id":101430651,"user":{"login":"lukasgeiter","id":5907946,"avatar_url":"https://avatars.githubusercontent.com/u/5907946?v=3","gravatar_id":"","url":"https://api.github.com/users/lukasgeiter","html_url":"https://github.com/lukasgeiter","followers_url":"https://api.github.com/users/lukasgeiter/followers","following_url":"https://api.github.com/users/lukasgeiter/following{/other_user}","gists_url":"https://api.github.com/users/lukasgeiter/gists{/gist_id}","starred_url":"https://api.github.com/users/lukasgeiter/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/lukasgeiter/subscriptions","organizations_url":"https://api.github.com/users/lukasgeiter/orgs","repos_url":"https://api.github.com/users/lukasgeiter/repos","events_url":"https://api.github.com/users/lukasgeiter/events{/privacy}","received_events_url":"https://api.github.com/users/lukasgeiter/received_events","type":"User","site_admin":false},"created_at":"2015-05-12T21:45:06Z","updated_at":"2015-05-12T21:45:06Z","body":"I agree with @Douglasdc3, this has been fixed with [this commit](https://github.com/laravel/framework/commit/1e80162e9a3a4c42a0079bd9f34e785197a66c07) and can be closed."}}