jeudi 19 avril 2018

How to issue query in Laravel?

I need create query in Laravel for chart.

I need create this json file:

For example: (30 days ago)

[{
            "day": "23",
            "count": 20
        }, {
            "day": "22",
            "visits": 10
        }, {
            "day": "21",
            "visits": 12
        }, {
            "day": "20",
            "visits": 30
        }, {
            "day": "19",
            "visits": 20
        }, {
            "day": "18",
            "visits": 10
        }, 
         .
         .
         .
         .
        }, {
            "day": "22",
            "visits": 10
        }]

My code in Laravel is:

 $date = Carbon::today()->subDays(30);
 $ordersChart = Order::selectRaw('dayname(created_at) day, count(*) count')
        ->where('created_at', '>=', $date)
        ->groupBy('day')
        ->latest()
        ->get();

But after return $ordersChart show this error:

SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #1 of ORDER BY clause is not in GROUP BY clause and contains nonaggregated column 'laravel.orders.created_at' which is not functionally dependent on columns in GROUP BY clause; this is incompatible with sql_mode=only_full_group_by (SQL: select dayname(created_at) day, count(*) count from `orders` where `created_at` >= 2018-03-20 00:00:00 group by `day` order by `created_at` desc)

How to issue this problem?



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2qJx3vK
via IFTTT

Aucun commentaire:

Enregistrer un commentaire