Peter
Peter

Reputation: 2702

How change all dates in array into date without time - laravel

I have a collection of a model in $object.

Then, for the purpose of delivering data to charts.js i get the dates:

var labels = {!! $object->pluck('created_at') !!};

The content looks like this: ["2014-06-21 13:22:14","2014-06-28 07:42:26","2016-02-17 17:39:21"]

How to properly transform the array so taht it contains only dates in format("Y-m-d") format?

["2014-06-21","2014-06-28","2016-02-17"]

For your convenience: http://nnnick.github.io/Chart.js/docs-v2

Upvotes: 1

Views: 504

Answers (1)

CharlieJade
CharlieJade

Reputation: 1183

In your model

class MyModel extends Model
{
    /**
     * Get created_at in date format ==> 1975-12-25
     *
     * @param  string  $value
     * @return string
     */
    public function getCreatedAtDateAttribute($value)
    {
        return $this->created_at->toDateString();
    }
}

then

var labels = {!! $object->pluck('created_at_date') !!};

Upvotes: 1

Related Questions