{"action":"created","issue":{"url":"https://api.github.com/repos/laravel/framework/issues/25627","repository_url":"https://api.github.com/repos/laravel/framework","labels_url":"https://api.github.com/repos/laravel/framework/issues/25627/labels{/name}","comments_url":"https://api.github.com/repos/laravel/framework/issues/25627/comments","events_url":"https://api.github.com/repos/laravel/framework/issues/25627/events","html_url":"https://github.com/laravel/framework/issues/25627","id":360518515,"node_id":"MDU6SXNzdWUzNjA1MTg1MTU=","number":25627,"title":"Transaction should ROLLBACK on False result","user":{"login":"sharifzadesina","id":15300551,"node_id":"MDQ6VXNlcjE1MzAwNTUx","avatar_url":"https://avatars2.githubusercontent.com/u/15300551?v=4","gravatar_id":"","url":"https://api.github.com/users/sharifzadesina","html_url":"https://github.com/sharifzadesina","followers_url":"https://api.github.com/users/sharifzadesina/followers","following_url":"https://api.github.com/users/sharifzadesina/following{/other_user}","gists_url":"https://api.github.com/users/sharifzadesina/gists{/gist_id}","starred_url":"https://api.github.com/users/sharifzadesina/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sharifzadesina/subscriptions","organizations_url":"https://api.github.com/users/sharifzadesina/orgs","repos_url":"https://api.github.com/users/sharifzadesina/repos","events_url":"https://api.github.com/users/sharifzadesina/events{/privacy}","received_events_url":"https://api.github.com/users/sharifzadesina/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2018-09-15T07:58:33Z","updated_at":"2018-09-15T14:49:46Z","closed_at":null,"author_association":"NONE","body":"The database transaction should rollback on false result,\r\nImagine this is our model:\r\n```php\r\nclass User extends Model\r\n{\r\n    public static function boot()\r\n    {\r\n        static::saving(function () {\r\n            return false;\r\n        });\r\n    }\r\n}\r\n```\r\nAnd the transaction:\r\n```php\r\n$saved = DB::transaction(function () {\r\n    $saved = true;\r\n    $saved = $saved && $anotherModel->save();\r\n    $saved = $saved && $user->save();\r\n    return $saved;\r\n});\r\n```\r\nAs you know the `$anotherModel->save()` will work but the `$user->save()` will fail without any exception, so the transaction won't rollback."},"comment":{"url":"https://api.github.com/repos/laravel/framework/issues/comments/421581611","html_url":"https://github.com/laravel/framework/issues/25627#issuecomment-421581611","issue_url":"https://api.github.com/repos/laravel/framework/issues/25627","id":421581611,"node_id":"MDEyOklzc3VlQ29tbWVudDQyMTU4MTYxMQ==","user":{"login":"thecrypticace","id":614993,"node_id":"MDQ6VXNlcjYxNDk5Mw==","avatar_url":"https://avatars2.githubusercontent.com/u/614993?v=4","gravatar_id":"","url":"https://api.github.com/users/thecrypticace","html_url":"https://github.com/thecrypticace","followers_url":"https://api.github.com/users/thecrypticace/followers","following_url":"https://api.github.com/users/thecrypticace/following{/other_user}","gists_url":"https://api.github.com/users/thecrypticace/gists{/gist_id}","starred_url":"https://api.github.com/users/thecrypticace/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/thecrypticace/subscriptions","organizations_url":"https://api.github.com/users/thecrypticace/orgs","repos_url":"https://api.github.com/users/thecrypticace/repos","events_url":"https://api.github.com/users/thecrypticace/events{/privacy}","received_events_url":"https://api.github.com/users/thecrypticace/received_events","type":"User","site_admin":false},"created_at":"2018-09-15T14:49:46Z","updated_at":"2018-09-15T14:49:46Z","author_association":"CONTRIBUTOR","body":"**This would be a breaking change.**\r\n\r\nI also will say that if you feel that this is a pattern you have to use there's a structural problem with your code or your approach. Transactions only rollback when an error occurs. Returning false from a model observer prevents it from being saved, this is not _necessarily_ indicative of an error. I recommend throwing an exception instead if you are really running into an error which must rollback the containing transaction.\r\n\r\nIf you **must** take your outlined approach then throw an exception when you detect result is `false`. However, I will maintain that this is likely to be indicative of a problem with the approach you're taking to solve whatever it is you're encountering.\r\n\r\n```php\r\ntry {\r\n    $saved = DB::transaction(function () {\r\n        $saved = true;\r\n        $saved = $saved && $anotherModel->save();\r\n        $saved = $saved && $user->save();\r\n\r\n        if (! $saved) {\r\n            throw new RuntimeException(\"Unable to save data\");\r\n        }\r\n\r\n        return $saved;\r\n    });\r\n} catch (RuntimeException $e) {\r\n    $saved = false;\r\n}\r\n```"}}
{"actor":{"display_login":"thecrypticace"}}