Eloquent Accessors: Dates, Casts, and "Wrong Way"

  Рет қаралды 9,642

Laravel Daily

Laravel Daily

Күн бұрын

Пікірлер: 20
@SXsoft99
@SXsoft99 11 ай бұрын
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
@faridmoreno2864
@faridmoreno2864 11 ай бұрын
Hi, Do you know some video where explain this (enum with date formats)? Sorry for my English 😅
@OkanUltimatum
@OkanUltimatum 11 ай бұрын
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
@wowdesigns7436
@wowdesigns7436 11 ай бұрын
I also format is like this ..
@silas4752
@silas4752 8 ай бұрын
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
@normanhuth478
@normanhuth478 11 ай бұрын
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.
@dutchman1990
@dutchman1990 11 ай бұрын
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.
@alnahian2003
@alnahian2003 11 ай бұрын
Great! I've started watching your videos regularly after 6 months! It seems like there are so many vids left to watch! :p Hahaha!
@serhiicho
@serhiicho 11 ай бұрын
Useful advices
@julienSibille
@julienSibille 11 ай бұрын
I like create custom attributes, and if i need it when json_encode just add it to appends
@logudotcom
@logudotcom 11 ай бұрын
Thanks for sharing. Useful.
@PabloZagni
@PabloZagni 11 ай бұрын
Excelent 100%
@aleksandarstevanovic5854
@aleksandarstevanovic5854 11 ай бұрын
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
@jorni6324
@jorni6324 11 ай бұрын
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
@acirinelli
@acirinelli 11 ай бұрын
This is how I do it, at least I’m doing something right ha
@mori181186
@mori181186 11 ай бұрын
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.
Eloquent Observers or Events Listeners? Which is Better?
8:24
Laravel Daily
Рет қаралды 61 М.
Top 5 Laravel "Bad Practices" (My Opinion)
10:32
Laravel Daily
Рет қаралды 25 М.
Гениальное изобретение из обычного стаканчика!
00:31
Лютая физика | Олимпиадная физика
Рет қаралды 4,8 МЛН
СИНИЙ ИНЕЙ УЖЕ ВЫШЕЛ!❄️
01:01
DO$HIK
Рет қаралды 3,3 МЛН
So Cute 🥰 who is better?
00:15
dednahype
Рет қаралды 19 МЛН
Как использовать очереди (Queues/Jobs) в Laravel
13:46
Просто о сложном. CutCode
Рет қаралды 24 М.
Eloquent where(): 6 "Tricks" You May Not Know
5:44
Laravel Daily
Рет қаралды 11 М.
Laravel Security: Top 7 Mistakes Developers Make
11:16
Laravel Daily
Рет қаралды 88 М.
Laravel: The BEST way to handle exceptions
9:29
Przemysław Przyłucki
Рет қаралды 18 М.
Microservices with Databases can be challenging...
20:52
Software Developer Diaries
Рет қаралды 122 М.
Eloquent Polymorphic Relations: Properly Explained
9:56
Laravel Daily
Рет қаралды 41 М.
Laravel Scout with typesense
7:09
Laravel
Рет қаралды 12 М.
Why Are Open Source Alternatives So Bad?
13:06
Eric Murphy
Рет қаралды 738 М.
The New Way to Define Eloquent Accessors and Mutators
8:38
Laracasts
Рет қаралды 4,6 М.
NEW: Laravel Project Examples - 5 CRUDs with Different Stack
12:27
Laravel Daily
Рет қаралды 1,5 М.
Гениальное изобретение из обычного стаканчика!
00:31
Лютая физика | Олимпиадная физика
Рет қаралды 4,8 МЛН