{"action":"created","issue":{"url":"https://api.github.com/repos/laravel/framework/issues/10245","labels_url":"https://api.github.com/repos/laravel/framework/issues/10245/labels{/name}","comments_url":"https://api.github.com/repos/laravel/framework/issues/10245/comments","events_url":"https://api.github.com/repos/laravel/framework/issues/10245/events","html_url":"https://github.com/laravel/framework/pull/10245","id":106140103,"number":10245,"title":"[5.1] Fix Pivot Timestamps Convertion to Carbon When Is Filled","user":{"login":"uxweb","id":5134732,"avatar_url":"https://avatars.githubusercontent.com/u/5134732?v=3","gravatar_id":"","url":"https://api.github.com/users/uxweb","html_url":"https://github.com/uxweb","followers_url":"https://api.github.com/users/uxweb/followers","following_url":"https://api.github.com/users/uxweb/following{/other_user}","gists_url":"https://api.github.com/users/uxweb/gists{/gist_id}","starred_url":"https://api.github.com/users/uxweb/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/uxweb/subscriptions","organizations_url":"https://api.github.com/users/uxweb/orgs","repos_url":"https://api.github.com/users/uxweb/repos","events_url":"https://api.github.com/users/uxweb/events{/privacy}","received_events_url":"https://api.github.com/users/uxweb/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/laravel/framework/labels/database","name":"database","color":"eb6420"},{"url":"https://api.github.com/repos/laravel/framework/labels/requires%20changes","name":"requires changes","color":"fef2c0"}],"state":"closed","locked":false,"assignee":null,"milestone":null,"comments":3,"created_at":"2015-09-12T07:47:08Z","updated_at":"2015-09-12T19:51:04Z","closed_at":"2015-09-12T19:50:43Z","pull_request":{"url":"https://api.github.com/repos/laravel/framework/pulls/10245","html_url":"https://github.com/laravel/framework/pull/10245","diff_url":"https://github.com/laravel/framework/pull/10245.diff","patch_url":"https://github.com/laravel/framework/pull/10245.patch"},"body":"This is related to Issue\r\n[#10243](https://github.com/laravel/framework/issues/10243).\r\n\r\nWhen an instance of Pivot is created and date attributes such as\r\ncreated_at and updated_at are passed, it tries to convert those to a\r\nCarbon instance using the connection’s sql grammar from the parent.\r\n\r\nAt the time the pivot is being filled, no table neither connection is\r\nalready set by the parent model, so the pivot resolves a grammar from\r\nthe default connection.\r\n\r\nThis happens when you have models that use a MySql Connection and other models that use other connection type (SqlServer, Postgres…).\r\n\r\nThe fix consist on moving `$this->setTable($table);` and\r\n`$this->setConnection($parent->getConnectionName());` before\r\n`$this->forceFill($attributes);` on the Pivot’s class constructor.\r\n\r\nI tried to change the testPropertiesAreSetCorrectly test in\r\nDatabaseEloquentPivotTest.php to reproduce the use case, as the current\r\ntest does not test that.\r\n\r\nBut got an error from PHP Unit pointing to this method in the Model\r\nclass:\r\n\r\n```\r\n1) DatabaseEloquentPivotTest::testPropertiesAreSetCorrectly\r\nBadMethodCallException: Method Mockery_33_Illuminate_Database_Query_Grammars_Grammar::getDateFormat() does not exist on this mock object\r\n```\r\n\r\n```\r\n/**\r\n * Get the format for database stored dates.\r\n *\r\n * @return string\r\n */\r\nprotected function getDateFormat()\r\n{\r\n    return $this->dateFormat ?:\r\n$this->getConnection()->getQueryGrammar()->getDateFormat();\r\n}\r\n```\r\n\r\nI think this is related to the mocking instance, but my testing\r\nknowledge is not enough to fix it.\r\n\r\nHope this helps!, if you need more details i am able to give you if\r\nneeded."},"comment":{"url":"https://api.github.com/repos/laravel/framework/issues/comments/139814420","html_url":"https://github.com/laravel/framework/pull/10245#issuecomment-139814420","issue_url":"https://api.github.com/repos/laravel/framework/issues/10245","id":139814420,"user":{"login":"taylorotwell","id":463230,"avatar_url":"https://avatars.githubusercontent.com/u/463230?v=3","gravatar_id":"","url":"https://api.github.com/users/taylorotwell","html_url":"https://github.com/taylorotwell","followers_url":"https://api.github.com/users/taylorotwell/followers","following_url":"https://api.github.com/users/taylorotwell/following{/other_user}","gists_url":"https://api.github.com/users/taylorotwell/gists{/gist_id}","starred_url":"https://api.github.com/users/taylorotwell/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/taylorotwell/subscriptions","organizations_url":"https://api.github.com/users/taylorotwell/orgs","repos_url":"https://api.github.com/users/taylorotwell/repos","events_url":"https://api.github.com/users/taylorotwell/events{/privacy}","received_events_url":"https://api.github.com/users/taylorotwell/received_events","type":"User","site_admin":false},"created_at":"2015-09-12T19:51:04Z","updated_at":"2015-09-12T19:51:04Z","body":"I fixed the test."}}