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.
Jun 30, 2020
6
Level 1
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 ???
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
Please or to participate in this conversation.