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();
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果