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

jamalroger's avatar

Laravel 7: notification timestamps serialization

Hi guys correctly i am upgrading from from laravel 6 to 7 but laravel 7 they changed default format date from y:m:d h:m:s to something like y:m:d h:m:s00000z I found i solution to casts timestamps in Model but where can i casts timestamps in notification ???

0 likes
6 replies
bobbybouwmann's avatar

I'm not sure what you mean with the dates in your notifications? If your notification uses a model, it should already use the correct casting based on the casting of the model.

MichalOravec's avatar
Level 75

Create Notification model as app/Notification.php

<?php

namespace App;

use DateTimeInterface;
use Illuminate\Notifications\DatabaseNotification;

class Notification extends DatabaseNotification
{
    /**
     * Prepare a date for array / JSON serialization.
     *
     * @param  \DateTimeInterface  $date
     * @return string
     */
    protected function serializeDate(DateTimeInterface $date)
    {
        return $date->format('Y-m-d H:i:s');
    }
}

Create Notifiable trait as app/Traits/Notifiable.php

<?php

namespace App\Traits;

use App\Notification;
use Illuminate\Notifications\Notifiable as BaseNotifiable;

trait Notifiable
{
    use BaseNotifiable;

    /**
     * Get the entity's notifications.
     *
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
     */
    public function notifications()
    {
        return $this->morphMany(Notification::class, 'notifiable')->orderBy('created_at', 'desc');;
    }
}

In your User model use that Notifiable trait

<?php

namespace App;

use App\Traits\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    // ...
}
2 likes
jamalroger's avatar

@michaloravec any other solution ??

@bobbybouwmann i have make notification from artisan command

php artisan make:notification Notification // to create notification
php artisan notification:table // to create  migration 

i don't where is notification model

i have just notification class in app/notification and migration

i am sending ajax to return notification in controller

	return $auth->unreadNotifications;

any idea ??

MichalOravec's avatar

@jamalroger Laravel use DatabaseNotification behind the scene so you have to replace it, how I mentioned before. So there is no other solution.

2 likes
Abdullah karam mohamed karam's avatar

@jamalroger as @michaloravec mentioned before this is the best solution but you can use transform method on collection notifications and return what you want

    $notifications = $this->admin->getNotifications()->simplePaginate(10);
    $notifications->transform(function ($notification) {
        return [
            'id' => $notification->id,
            'data' => $notification->data,
            'created_at' => $notification->created_at->diffForHumans(),
        ];
    });

Please or to participate in this conversation.