michal michal - 1 year ago 69
PHP Question

How to delete duplicate date

I have a project in Laravel. I want returned date with table "transmits" with column "created_at". This column is type


enter image description here

When I returned date on my site I have this:

Select date: 2016-08-24 2016-08-24 2016-08-24 2016-08-24 2016-08-25

I want the date to be displayed individually. Like this:

Select date: 2016-08-24 2016-08-25

My code:

print $dayDate = (Carbon::createFromFormat('Y-m-d H:i:s', $days->created_at)->format('Y-m-d'));

I can't using
in query to databse, because
column is the date and hour. The hour differ from each other, therefore I can not use a
. I can't also use
, because variable
returned this when i using

string(10) "2016-08-24" string(10) "2016-08-24" string(10)
"2016-08-24" string(10) "2016-08-24" string(10) "2016-08-25"
string(10) "2016-08-25"

Each date is a separate table. I do not know how I can combine them into a single table.

In file view:

@foreach($day as $days)
$dayDate = (Carbon::createFromFormat('Y-m-d H:i:s', $days->created_at)->format('Y-m-d'));
var_dump ($dayDate);


And in file controller:

$day = transmit::select('created_at')->groupBy('created_at')->get();

My post is not duplicate this post [MySQL Query GROUP BY day / month / year][2]

[2]: MySQL Query GROUP BY day / month / year, because in my column
is date, hour, munte and secon, therefore I can not use the groupBy.

Answer Source

Use the following expression

$day = transmit::selectRaw('date(created_at) as created_date')->groupBy('created_date')->get();

One drawback is mysql can't use index on the field created_at so it will slow down the query.