{"action":"created","issue":{"url":"https://api.github.com/repos/laravel/internals/issues/262","repository_url":"https://api.github.com/repos/laravel/internals","labels_url":"https://api.github.com/repos/laravel/internals/issues/262/labels{/name}","comments_url":"https://api.github.com/repos/laravel/internals/issues/262/comments","events_url":"https://api.github.com/repos/laravel/internals/issues/262/events","html_url":"https://github.com/laravel/internals/issues/262","id":184007458,"number":262,"title":"Handling option dates","user":{"login":"TheShiftExchange","id":1210658,"avatar_url":"https://avatars.githubusercontent.com/u/1210658?v=3","gravatar_id":"","url":"https://api.github.com/users/TheShiftExchange","html_url":"https://github.com/TheShiftExchange","followers_url":"https://api.github.com/users/TheShiftExchange/followers","following_url":"https://api.github.com/users/TheShiftExchange/following{/other_user}","gists_url":"https://api.github.com/users/TheShiftExchange/gists{/gist_id}","starred_url":"https://api.github.com/users/TheShiftExchange/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/TheShiftExchange/subscriptions","organizations_url":"https://api.github.com/users/TheShiftExchange/orgs","repos_url":"https://api.github.com/users/TheShiftExchange/repos","events_url":"https://api.github.com/users/TheShiftExchange/events{/privacy}","received_events_url":"https://api.github.com/users/TheShiftExchange/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-10-19T16:11:54Z","updated_at":"2016-10-20T13:50:47Z","closed_at":null,"body":"Is there a better way Eloquent could handle 'optional' dates?\r\n\r\nLets say we have a `users` model with a `dob` (date of birth) column that is optional.\r\n\r\nFirst of all - we need to be able to handle the input optionally. If you dont, you get a `InvalidArgumentException - Data missing` error.\r\n\r\nSo I find myself duplicating this type of code in my models:\r\n\r\n```\r\nclass User extends Eloquent {\r\n\r\n    protected $table = 'user';\r\n    protected $dates = ['dob'];\r\n\r\n    public function setDobAttribute($value)\r\n    {\r\n        if (strlen($value)) {\r\n            $this->attributes['dob'] = Carbon::createFromFormat('d/m/Y', $value);\r\n         } else {\r\n             $this->attributes['dob'] = null;\r\n         }\r\n    }\r\n}\r\n```\r\n\r\nThen separately when trying to display that data - this will not work:\r\n\r\n```\r\n@foreach (User::all() as $user)\r\n    {{ $user->dob->format('d/m/Y') }}\r\n@endforeach\r\n```\r\n\r\n...because you will get a `Call to a member function format() on a non-object error` if the `dob` column is null.\r\n\r\nSo that means you need to always pass this variable into a helper to filter it for `null` values, or you need to manually add `getter` methods for all your date variables.\r\n\r\nI find I come across this issue a lot. And googling around it seems that other people do to. Surely there has to be a better way?"},"comment":{"url":"https://api.github.com/repos/laravel/internals/issues/comments/255111377","html_url":"https://github.com/laravel/internals/issues/262#issuecomment-255111377","issue_url":"https://api.github.com/repos/laravel/internals/issues/262","id":255111377,"user":{"login":"Garbee","id":868301,"avatar_url":"https://avatars.githubusercontent.com/u/868301?v=3","gravatar_id":"","url":"https://api.github.com/users/Garbee","html_url":"https://github.com/Garbee","followers_url":"https://api.github.com/users/Garbee/followers","following_url":"https://api.github.com/users/Garbee/following{/other_user}","gists_url":"https://api.github.com/users/Garbee/gists{/gist_id}","starred_url":"https://api.github.com/users/Garbee/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Garbee/subscriptions","organizations_url":"https://api.github.com/users/Garbee/orgs","repos_url":"https://api.github.com/users/Garbee/repos","events_url":"https://api.github.com/users/Garbee/events{/privacy}","received_events_url":"https://api.github.com/users/Garbee/received_events","type":"User","site_admin":false},"created_at":"2016-10-20T13:50:47Z","updated_at":"2016-10-20T13:50:47Z","body":"It is difficult to abstract something like this away. If you have `null` stored for the date, then what is the date/time you put into the carbon instance? You *need* to have *something* stored within it, not null. You then would have no real way of knowing if what you are calling for is the actual stored data or the \"default\" data for `null`.\r\n\r\nI think in terms of Laravel, this is a non-issue that you need to find your own way to abstract out in your application. (Personally, I use twig so the date filter there so null just doesn't output anything.)\r\n\r\nHowever, this discussion may be something to bring up with Carbon to see if anyone there has an idea for how a nulled value within a carbon instance can be handled cleanly. If Carbon builds a way to handle this so you can call methods against an object with no data, then Laravel could later upgrade to use that and handle the storage piece transparently within Eloquent."}}
{"actor":{"display_login":"Garbee"}}