Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

hjortur17's avatar

How to modify the created_at date time through the controller

Hi, can someone help figure out how to transform created_at when I select my $post like this:

$post = $tag->posts()->where('slug', $slug)->with('author')->with('tags')->firstOrFail();

I have usually been doing it by using transform but I'm not sure how and if it's possible when doing the select query like this. So any ideas?

'created_at' => Carbon::createFromFormat('Y-m-d H:i:s', $post->created_at)->locale('is_IS')->isoFormat('Do MMM YYYY'),
0 likes
10 replies
Nakov's avatar

So in order to update the value, you will have to either add the created_at field in your $fillable array of the model, or just turn the timestamps off for this query.

$post->timestamps = false;
$post->created_at = Carbon::createFromFormat('Y-m-d H:i:s', $post->created_at)->locale('is_IS')->isoFormat('Do MMM YYYY');
$post->save();
1 like
hjortur17's avatar

@Nakov - Sorry but not looking for how I would update it, only display it.

Nakov's avatar

@hjortur17 Then why don't you do this only:

$post->created_at->locale('is_IS')->isoFormat('Do MMM YYYY');
1 like
hjortur17's avatar

@Nakov - I'm using VueJS, so somehow I would need to transform this on the backend.

Snapey's avatar

Are you saying you want to change the created_at value in the database or just change the way it is displayed?

Snapey's avatar

@hjortur17 So its normally cast to a Carbon instance, why are you converting it again?

hjortur17's avatar

@Snapey - I want to have it on this format Do MMM YYYY and really don't want to be installing and importing DayJS or MomentJs to format the string in my Vue component.

I have been doing this like this:

public function index() {
        return Inertia::render('Homepage', [
            'posts' => Post::query()
                    ->when(Request::input('leita'), function ($query, $search) {
                        $query->where('title', 'like', "%{$search}%");
                    })
                    ->with('tags')
                    ->get()
                    ->transform(function ($post) {
                        return [
                            'slug' => $post->slug,
                            'title' => $post->title,
                            'author' => $post->author,
                            'created_at' => Carbon::createFromFormat('Y-m-d H:i:s', $post->created_at)->locale('is_IS')->isoFormat('Do MMM YYYY'),
                            'content' => $post->content,
                            'img_url' => $post->img_url,
                            'tags' => $post->tags
                        ];
                    }),
            'filters' => Request::only(['search'])

                    // Ef ég bætti við ->paginate(*), þá þarf ég að gera eftir það ->withQueryString()
        ]);
    }

But I'm not sure how I can format the date when I have my query like this:

$post = $tag->posts()->where('slug', $slug)->with('author')->with('tags')->firstOrFail();

        return Inertia::render('Post', [
            'post' => $post
        ]);
Snapey's avatar

i don't understand the issue, or why

'created_at' => Carbon::createFromFormat('Y-m-d H:i:s', $post->created_at)->locale('is_IS')->isoFormat('Do MMM YYYY'),

is not just

'created_at'  => $post->created_at->locale('is_IS')->isoFormat('Do MMM YYYY'),
1 like
hjortur17's avatar
hjortur17
OP
Best Answer
Level 14

I added this to my model instead of being formatting the string everywhere:

public function getCreatedAtAttribute($date)
    {
        return Carbon::createFromFormat('Y-m-d H:i:s', $this->attributes['created_at'])->locale('is_IS')->isoFormat('Do MMM YYYY');
    }

Please or to participate in this conversation.