i recomend using the "protected $appends = ['created_at_formated'];" in your model and make a mutator on that also i recomend creating an enum with all your date formats as constants and use that everywhere in the project so that when the client wants a different format you just modify in one place
@faridmoreno286411 ай бұрын
Hi, Do you know some video where explain this (enum with date formats)? Sorry for my English 😅
@OkanUltimatum11 ай бұрын
I usually create a separate helpers.php file and add a formatDate(Carbon $date, $format = 'd/m/Y') method in there. Benefits: 1. I can still use $model->created_at as a Carbon and do whatever I want 2. If I want to format a date I just call formatDate($model->created_at) 3. If I want a specific format I declare that in the 2nd param 4. If I want to change the format for the whole project I just update the 2nd param it in the formatDate method
@wowdesigns743611 ай бұрын
I also format is like this ..
@silas47528 ай бұрын
Hi, can somebody explain to me what's the advantage to use custom attributes instead of calling the function directly? Maybe he said it in the video but my english isn't good enough
@normanhuth47811 ай бұрын
I usually create a carbon macro to format a date for the frontend. And if I want to change the format, I just have to adjust the macro.
@dutchman199011 ай бұрын
In my case i had to set a different name for created_at field and returning a array with two different formats formatted value to show in frontend and timestamp for mobile apis so that app developer can use timestamp in their end.
@alnahian200311 ай бұрын
Great! I've started watching your videos regularly after 6 months! It seems like there are so many vids left to watch! :p Hahaha!
@serhiicho11 ай бұрын
Useful advices
@julienSibille11 ай бұрын
I like create custom attributes, and if i need it when json_encode just add it to appends
@logudotcom11 ай бұрын
Thanks for sharing. Useful.
@PabloZagni11 ай бұрын
Excelent 100%
@aleksandarstevanovic585411 ай бұрын
Personally i avoid accessors alltogether, i would rather declare a response layer and massage the data there when i need it, i shot myself in the foot once, i had slow accessor for whatever model i had, i had to have db query, include other tables to get the result... now, on the other side of the app that i dont need the data i show a list of the models and laravel for each one has extra queries which made it really slow
@jorni632411 ай бұрын
I always wrap my models in resources. That way, if I have have to format a date, i can just send the original next to a formatted date
@acirinelli11 ай бұрын
This is how I do it, at least I’m doing something right ha
@mori18118611 ай бұрын
In 99% you need only 1 format. If you need a other format in blade, then create a new attribute in your model. Otherwise you will repeat the formats in blade over and over again. 1 format for show the attribute, and the other for database and pickers.