0

I am making a API request but when i retrieve created_at then i get 2019-10-09T11:07:08.000000Z but in database it is 2019-10-09 11:07:08. How can i solve this issue? I don't want to use date formatting because i already have proper date format in my database. I just want what i have in database. Look at my database: https://prnt.sc/piljms

API Controller:

 public function singleGallery($id)
    {
        $gallery = Gallery::findOrFail($id);
        return new GalleryResource($gallery);
    }

Gallery Resource:

    return [
        'gallery_id' => $this->id,
        'created_date' => $this->created_at,
        'modified_date' => $this->updated_at
    ]
5
  • What's wrong with 2019-10-09T11:07:08.000000Z? What are you expecting? Commented Oct 13, 2019 at 9:15
  • @fox91 in database it is 2019-10-09 11:07:08 Commented Oct 13, 2019 at 9:20
  • I saw your edit, can you clarify? Do you expect a date in MySQL (or another DB) format? Commented Oct 13, 2019 at 9:22
  • I want date as it is in database stored. In database it is 2019-10-09 11:07:08 then why it is showing 2019-10-09T11:07:08.000000Z ? Commented Oct 13, 2019 at 9:24
  • Probably is the default settings in Laravel, you need to format the date as you need. Commented Oct 13, 2019 at 9:27

3 Answers 3

1

Using php's date and strtotime function you can format date as per your choice.

'created_date' => date('Y-m-d', strtotime($this->created_at))

I have formatted created_at in Year/month/date but you can do different formatting like d/m/Y for day/month/Year

If you would like to go Laravel way then you have to use Carbon library.

Sign up to request clarification or add additional context in comments.

2 Comments

I don't want to format it like you give here. My database have 2019-10-09 11:07:08 but why it is showing 2019-10-09T11:07:08.000000Z? I just want what i have in database.
@ChoncholMahmud When you hit any query, laravel at first run its own different modules/middlewares under the hood. In this case, laravel is using the date format = 'Y-m-d\TH:i:s.u'; internally before outputting. So, you need to format in your own way.
0

Go with this:

return [
    'gallery_id' => $this->id,
    'created_date' => $this->created_at->format('Y-m-d H:i:s'),
    'modified_date' => $this->updated_at->format('Y-m-d H:i:s'),
]

2 Comments

What the reason you use format() as i already have that format in my database? Take a look into my database: prnt.sc/piljms
It really depends on DB tecnology, but probably your date isn't saved formatted in that way by your DB, only display it in that way. Do you read the date from your database as "string" or as PHP DateTime (or similar) object? Maybe Laravel do it for you... Can you post the result of var_dump($this->created_at);?
0
<?php
    
    namespace App\Models;
    
    use Illuminate\Database\Eloquent\Factories\HasFactory;
    use Illuminate\Database\Eloquent\Model;
    use DateTimeInterface;
    
    class Notification extends Model
    {
        use HasFactory;
        
        protected $fillable = [
            'id',
            'created_at',
            'updated_at',
        ];
        
        protected function serializeDate(DateTimeInterface $date)
        {
            return $date->format('Y-m-d H:i:s');
        }
    }
    ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.