doulai2025 2018-08-21 10:33
浏览 60
已采纳

从上周的SQL和Laravel获取数据

I am currently working on a web application and using ChartJS. I need to get data from the last 7 days and count it. I explain:

I need to count the number of client that registered each days from the seven last days to display it with ChartJS.

I have some problems with the SQL Request.

Get today's date:

$jj = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d"), date("Y")));

In my database, i have a 'created_at' column with timestamp like this :

2018-08-21 11:07:59

For the request, I followed some tutorials and did this:

SELECT count(client.id) FROM parcel WHERE created_at = :dateclient, array('dateclient' => $jj);

But the request isn't working, I assume that there are some errors. If some pros knows how to do it !

By the way: I did the request for one date for the moment, but I will need to do it for seven days

  • 写回答

1条回答 默认 最新

  • dounie0889 2018-08-21 10:56
    关注

    Please use eloquent query to overcome SQL Injection like following

    Here I am assuming you have Models folder inside App folder else you can directly use Parcel:: without Models\Parcel

    $lastWeek = date('Y-m-d', strtotime("-7 day", date('Y-m-d')));
    /* In the following code you can directly get the count value */
    $parcelCount  = Models\Parcel::whereDate('created_at', $lastWeek)->count();
    

    OR using DB::raw

    $parcelCount = Models\Parcel::select(\DB::raw("COUNT(id) as parcel_count"))
        ->whereDate('created_at', $lastWeek)
        ->first();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 使用Photon PUN2解决游戏得分同步的问题
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了