{"action":"created","issue":{"url":"https://api.github.com/repos/laravel/framework/issues/10392","labels_url":"https://api.github.com/repos/laravel/framework/issues/10392/labels{/name}","comments_url":"https://api.github.com/repos/laravel/framework/issues/10392/comments","events_url":"https://api.github.com/repos/laravel/framework/issues/10392/events","html_url":"https://github.com/laravel/framework/pull/10392","id":108479495,"number":10392,"title":"[5.2] Refresh new models after saving them to the database","user":{"login":"adamwathan","id":4323180,"avatar_url":"https://avatars.githubusercontent.com/u/4323180?v=3","gravatar_id":"","url":"https://api.github.com/users/adamwathan","html_url":"https://github.com/adamwathan","followers_url":"https://api.github.com/users/adamwathan/followers","following_url":"https://api.github.com/users/adamwathan/following{/other_user}","gists_url":"https://api.github.com/users/adamwathan/gists{/gist_id}","starred_url":"https://api.github.com/users/adamwathan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/adamwathan/subscriptions","organizations_url":"https://api.github.com/users/adamwathan/orgs","repos_url":"https://api.github.com/users/adamwathan/repos","events_url":"https://api.github.com/users/adamwathan/events{/privacy}","received_events_url":"https://api.github.com/users/adamwathan/received_events","type":"User","site_admin":false},"labels":[],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":21,"created_at":"2015-09-26T16:42:51Z","updated_at":"2015-09-27T15:18:03Z","closed_at":"2015-09-26T17:47:42Z","pull_request":{"url":"https://api.github.com/repos/laravel/framework/pulls/10392","html_url":"https://github.com/laravel/framework/pull/10392","diff_url":"https://github.com/laravel/framework/pull/10392.diff","patch_url":"https://github.com/laravel/framework/pull/10392.patch"},"body":"Currently when a new model is created or saved, it does not honor database default values for optional fields that were not set before saving the model.\r\n\r\nFor example, if you had a table like this:\r\n\r\n```php\r\nSchema::create('users', function ($table) {\r\n    $table->increments('id');\r\n    $table->string('email')->unique();\r\n    $table->boolean('is_admin')->default(false);\r\n    $table->timestamps();\r\n});\r\n```\r\n\r\n...and created a new user like this:\r\n\r\n```php\r\n$user = User::create(['email' => 'jennifer@example.com']);\r\n```\r\n\r\n...then `$user->is_admin` would be `null`, not `false`.\r\n\r\nThis change adds a `refresh` method to the `Model` class and calls it at the end of saving a newly created model to ensure that the model's attributes in memory reflect the values that are in the database.\r\n\r\nIt's unfortunate that it needs to execute a second query when creating the model, but I'm not sure what a better solution would be. Open to feedback."},"comment":{"url":"https://api.github.com/repos/laravel/framework/issues/comments/143567137","html_url":"https://github.com/laravel/framework/pull/10392#issuecomment-143567137","issue_url":"https://api.github.com/repos/laravel/framework/issues/10392","id":143567137,"user":{"login":"adamwathan","id":4323180,"avatar_url":"https://avatars.githubusercontent.com/u/4323180?v=3","gravatar_id":"","url":"https://api.github.com/users/adamwathan","html_url":"https://github.com/adamwathan","followers_url":"https://api.github.com/users/adamwathan/followers","following_url":"https://api.github.com/users/adamwathan/following{/other_user}","gists_url":"https://api.github.com/users/adamwathan/gists{/gist_id}","starred_url":"https://api.github.com/users/adamwathan/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/adamwathan/subscriptions","organizations_url":"https://api.github.com/users/adamwathan/orgs","repos_url":"https://api.github.com/users/adamwathan/repos","events_url":"https://api.github.com/users/adamwathan/events{/privacy}","received_events_url":"https://api.github.com/users/adamwathan/received_events","type":"User","site_admin":false},"created_at":"2015-09-27T15:18:03Z","updated_at":"2015-09-27T15:18:03Z","body":"@lukasgeiter Total agreement. I'm starting to look at it sort of like foreign key constraints. You can still set up your relationships in your code without actually having real foreign keys, and it would be nice to set up model defaults without having to set up database level defaults. You still add those things in the database for the purpose of data integrity in your particular data store, but it's an implementation detail of the database you've decided to use, and shouldn't be depended on by your actual application logic.\r\n\r\nI think I would be happy with having a `$defaults` property in the model, and making `RefreshOnCreate` opt-in either through the trait @crynobone put together or a model property or something, solely to avoid always running that extra query when it may not be needed."}}