0

Hello great people of SO

I hope you all have a great day!

I have an issue with my project timestamps

I have 2 Models, News and Comments

During working on my project, I tried to replicate 'real world data'

So in my Seeder (NewsSeeder),

use Faker\Generator as Faker;
use App\News;
...

public function run(Faker $faker) {
    News::create([
        'source' => $faker->url
        'body' => $faker->paragraphs(random_int(1, 4), true)
    ]):
}

// Please note that, relationship between `News` and `Comments` is `hasMany`
// `News` hasMany `Comment`
// `Comment` belongs to a `User`

In my CommentSeeder

use Faker\Generator as Faker;
use App\News;
use App\Comment;
use App\User;
use Carbon\Carbon;
...

public function run() {
    foreach (News::all() as $news) {
        $i = 0;
        for ($i; $i < random_int(1, 5); $i++) { // This will generate comments for each `News` with random amount
            $news->comments()->create([
                'user_id' => User::inRandomOrder()->first()->id,
                'body' => $faker->paragraphs(random_int(1,3), true),
                'created_at' => Carbon::parse($news->created_at)->addMinutes(random_int(1, 999)) // Comments created after the news is posted, make sense right?
            ]);
        }
    }
}

Everything works perfectly like I want it to be

The problem is: I see 2 timestamps format

Example:

"news": [
    {
        "source": ...
        "body": ...
        "created_at": "2021-02-10 15:19:09" // This
        "comments": [
            {
                "user_id": ...
                "body": ...
                "created_at": "2021-02-10T16:22:33.000000Z" // and this
            },
            ...
            ...
        ]
    },
    ...
]

Q1:

Why they are different?

In model I'm using $table->timestamps();, which is Laravel's default timestamps format

Q2:

How to make all my models timestamps become like this: "2021-02-10T08:22:33.000000Z"

Thanks in advance

If there's any unclear explanation, I will edit a.s.a.p

Gwein
  • 109
  • 3
  • 14
  • laravel by default send `unix timestamp` may be you modify somewhere to other format – Kamlesh Paul Feb 10 '21 at 09:48
  • Hi, yes, I think I might have change it, In `CommentSeeder`, in this line: `Carbon::parse($news->created_at)->addMinutes(random_int(1, 999))` – Gwein Feb 10 '21 at 09:51
  • Because you are using laravel 7 or earlier version https://stackoverflow.com/a/63448356/4575350 – STA Feb 10 '21 at 09:52
  • How to make all my models timestamp become like comment model? like: `"2021-02-10T08:22:33.000000Z"` – Gwein Feb 10 '21 at 09:52
  • Hi @sta, yes I'm using laravel ver. 7, how to change format to unix timestamp? idk much about php – Gwein Feb 10 '21 at 09:56
  • @ChristianDelvianto you need to change it on your model, override the `serializeDate()` method – STA Feb 10 '21 at 09:57
  • 1
    Show us the relevant part (eg timestamps) from the migrations for **both** News and Comments models. And look in **both** models for relevant date/timestamp stuff, like any `$dates`, `$dateFormat`, `$casts`, etc. – Don't Panic Feb 10 '21 at 09:57
  • Hi, @Don'tPanic, I don't change the default table timestamps, nor $casts attribute in model, everything comes with Laravel's default – Gwein Feb 10 '21 at 10:00
  • @sta, `return $date-> ????;` – Gwein Feb 10 '21 at 10:02
  • Interesting! *I see 2 timestamps format* - where/how do you see that? I mean what code generates that dump, is it coming from DB? – Don't Panic Feb 10 '21 at 10:05
  • @Don'tPanic **I guess** my `CommentSeeder`, in line: `created_at` might have changed it, – Gwein Feb 10 '21 at 10:08
  • 2 timestamp are different, really weird. I am not sure where you need to change, on News or Comment model, may be on your Comment model – STA Feb 10 '21 at 10:10
  • @sta how about using mutator? `public function setCreatedAtAttribute($value) {}` and change the format to unix timestamp, but **idk what Carbon function** to change it to unix timestamp – Gwein Feb 10 '21 at 10:12
  • There is no Carbon function to change, here you assign a Carbon object to the comment date, and the output is good for comment. Here all the Carbon and comment stuff just make noise around your question which is: how to get "created_at": "2021-02-10T15:19:09.123456Z" instead of "created_at": "2021-02-10 15:19:09" in the "news" JSON output. And so we need the relevant part of the model and response to help. I suggest you to rewrite the question this way. But maybe at this point you'll find already asked similar questions. – KyleK Feb 10 '21 at 15:42
  • https://stackoverflow.com/a/54784868/2991319 – KyleK Feb 10 '21 at 15:45

1 Answers1

0

Add "->withTimestamps()" to your relation to add them.

public function comments() {
    return $this->hasMany(Comment::class)->withTimestamps();
}

In each model you want to change the format, add this:

protected $casts = [
    'created_at' => 'datetime:{format}',
];

Replace the {format} with the format you want. I think it is "c" or something.