Why not set it in session ?
Allowing custom date formats
So I'm running into an issue with my application - where users often enter "dates".
Currently my application allows 'd-m-Y' - but I need to allow 'm/d/Y' etc.
My thoughts are I can make this a configurable option for the user - and they state the date format in their config - and that is then used throughout the application. Something like this:
{{ $date->format(auth()->user()->date_format) }}
That would work for displays fairly well. But then I need to handle validation. Once again - I am thinking something like this in my FormRequest classes:
return [
'date' => 'required|string|date|date_format:'.auth()->user()->date_format,
'notes' => 'string|max:255',
];
So that way the validation is relative to the users setting.
However - it is the last bit that I'm stumped with. How do I then allow eloquent to safely store the date? Currently I do this with my fixed date format:
public function setDateAttribute($value)
{
$this->attributes['date'] = Carbon::createFromFormat('d-m-Y', $value);
}
One option I could do is this:
public function setDateAttribute($value)
{
$this->attributes['date'] = Carbon::createFromFormat(auth()->user()->date_format, $value);
}
...but I dont like the idea of putting auth() related functions into my Eloquent models - I know they dont belong there.
So how can I get around this? I need Eloquent to be "aware" of the date setting - so it stores the string correctly?
Is there a sanizitize option I could use somewhere before it hits Eloquent - so I could always send Eloquent a specific date format?
Please or to participate in this conversation.