Home » Php » php – What does `$protected dates` accomplish in Eloquent models

php – What does `$protected dates` accomplish in Eloquent models

Posted by: admin February 25, 2020 Leave a comment

Questions:

I was writing a simple test to evaluate saving a future publish date for a saved article. This is how I send data with publish date for tomorrow

    $response = $this->putJson('/api/blogs/' . $blogPost->slug, [
        'blog' => [
            'publish' => true,
            'publish_date' => Carbon::today()->addDays(1),
        ]
    ]);
    $response->assertStatus(200);

At the controller I save it as follows

    $blogData = $request->json()->all()['blog'];
    $blog->fill($blogData);
    $blog->save();

I get the following error while running the test

[2020-02-13 09:42:11] local.ERROR: Unexpected data found.
Trailing data {"userId":6,"exception":"[object] (InvalidArgumentException(code: 0): Unexpected data found.
Trailing data at /Users/anadi/Code/website/adminpanel/vendor/nesbot/carbon/src/Carbon/Traits/Creator.php:576)
[stacktrace]
#0 /Users/anadi/Code/website/adminpanel/vendor/nesbot/carbon/src/Carbon/Traits/Creator.php(598): Carbon\Carbon::rawCreateFromFormat('Y-m-d H:i:s', '2020-02-14T00:0...', NULL)
#1 /Users/anadi/Code/website/adminpanel/vendor/laravel/framework/src/Illuminate/Support/DateFactory.php(217): Carbon\Carbon::createFromFormat('Y-m-d H:i:s', '2020-02-14T00:0...')
#2 /Users/anadi/Code/website/adminpanel/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php(261): Illuminate\Support\DateFactory->__call('createFromForma...', Array)
#3 /Users/anadi/Code/website/adminpanel/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php(813): Illuminate\Support\Facades\Facade::__callStatic('createFromForma...', Array)
#4 /Users/anadi/Code/website/adminpanel/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php(835): Illuminate\Database\Eloquent\Model->asDateTime('2020-02-14T00:0...')
#5 /Users/anadi/Code/website/adminpanel/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php(580): Illuminate\Database\Eloquent\Model->fromDateTime('2020-02-14T00:0...')
#6 /Users/anadi/Code/website/adminpanel/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(329): Illuminate\Database\Eloquent\Model->setAttribute('publish_date', '2020-02-14T00:0...')
#7 /Users/anadi/Code/website/adminpanel/app/Http/Controllers/BlogPostController.php(102): Illuminate\Database\Eloquent\Model->fill(Array)

When I remove

protected $dates = ['deleted_at','publish_date'];

from the eloquent model, this error does not occur. Which brings me to my question, what does this line above do?

How to&Answers:

Actually this line turns those fields into Carbon objects and it will help laravel to know that these are date fields.
For example :
If you write this line in your model
protected $dates = [‘deleted_at’,’publish_date’];
You can use $model->publish_date->format(‘d/m/Y’) to format date.