Home > Software engineering >  Different date between sql and view
Different date between sql and view

Time:04-26

here is my problem I want to display in my view a date in the format 'Y-d-m H:i:s.u', it is inserted correctly in my database but when it is displayed in the view 2 years were added and days too.dd of my tickets

In my controller

    {
        $tickets = Ticket::where('user_id', Auth::user()->id)->paginate(10);
       // dd($tickets);

        return view('tickets.user_tickets', compact('tickets'));
    }

In my view <td> {{ $ticket->created_at }}</td>

Thank you to all those who can help me

CodePudding user response:

Use PHP date function to display in view:

date('F,d Y h:i:s' , strtotime($ticket->created_at))

CodePudding user response:

$ticket->created_at should be a Carbon instance, meaning you can format it as required via:

{{ $ticket->created_at->format('Y-d-m H:i:s.u') }}

Check the documentation:

https://carbon.nesbot.com/docs/#api-setters

If this doesn't work, make sure your Ticket.php model has it specified:

<?php

namespace App\Models;

class Ticket extends Model {
  protected $dates = ['created_at', 'updated_at'];
}

Anything in the protected $dates can chain the ->format() method, or any other Carbon method.

  • Related