获得dayname失败的计数-Laravel 9

发布于 2025-02-11 08:03:29 字数 3561 浏览 0 评论 0原文

就我的情况而言,我有一个数据表包含一个时间戳数据调用“ create_at”,现在,它想要获得时间戳的日名计数,

因此理想情况下它将就像

​想将其保存在阵列中,以使我将数据传递给May图表。JS图表 所以我要做的是,

 $getdate = DB::table('watchlists')
      ->select('created_at')
      ->get();

      $getweekday = Carbon::parse($getdate)->format('l');
         $dayarray=[];
      if($getweekday =='Monday')
      {
          $mondaycount = Carbon::parse($getdate)->count();

          $dayarray=[0];
      }
      elseif($getweekday =='Tuesday')
      {
        $tuesday = Carbon::parse($getdate)->count();
        $dayarray=[1];
      }
      elseif($getweekday =='Wednesday')
      {
        $wednesday = Carbon::parse($getdate)->count();
        $dayarray=[2];
      }
      elseif($getweekday =='Thursday')
      {
        $thursday = Carbon::parse($getdate)->count();
        $dayarray=[3];
      }
      elseif($getweekday =='Friday')
      {
        $firday = Carbon::parse($getdate)->count();
        $dayarray=[4];
      }
      elseif($getweekday =='Saturday')
      {
        $saturday = Carbon::parse($getdate)->count();
        $dayarray=[5];
      }
      elseif($getweekday =='Sunday')
      {
        $sunday = Carbon::parse($getdate)->count();
        $dayarray=[6];
      }
<script>
        const ctx = document.getElementById('chart1').getContext('2d');
        const myChart = new Chart(ctx, {
            type: 'bar',
            data: {
                labels: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
                datasets: [{
                    label: 'Visitor Volume by weekday',
                    data: [{{ $dayarray}}],
                    backgroundColor: [
                        'rgba(255, 99, 132, 0.2)',
                        'rgba(54, 162, 235, 0.2)',
                        'rgba(255, 206, 86, 0.2)',
                        'rgba(75, 192, 192, 0.2)',
                        'rgba(153, 102, 255, 0.2)',
                        'rgba(255, 159, 64, 0.2)'
                    ],
                    borderColor: [
                        'rgba(255, 99, 132, 1)',
                        'rgba(54, 162, 235, 1)',
                        'rgba(255, 206, 86, 1)',
                        'rgba(75, 192, 192, 1)',
                        'rgba(153, 102, 255, 1)',
                        'rgba(255, 159, 64, 1)'
                    ],
                    borderWidth: 1
                }]
            },
            options: {
                scales: {
                    y: {
                        beginAtZero: true
                    }
                }
            }
        });

但是当我尝试运行代码时,

Could not parse '[{"created_at":"2022-06-24 18:50:05"},{"created_at":"2022-06-26 13:24:42"},{"created_at":"2022-06-24 18:50:05"},{"created_at":"2022-06-26 13:24:42"},{"created_at":"2022-06-26 13:24:42"},{"created_at":"2022-06-26 13:24:42"}]': DateTime::__construct(): Failed to parse time string ([{&quot;created_at&quot;:&quot;2022-06-24 18:50:05&quot;},{&quot;created_at&quot;:&quot;2022-06-26 13:24:42&quot;},{&quot;created_at&quot;:&quot;2022-06-24 18:50:05&quot;},{&quot;created_at&quot;:&quot;2022-06-26 13:24:42&quot;},{&quot;created_at&quot;:&quot;2022-06-26 13:24:42&quot;},{&quot;created_at&quot;:&quot;2022-06-26 13:24:42&quot;}]) at position 0 ([): Unexpected character

我想问的是错误的错误,我错过了什么吗?

PS。我还想问如何将数据传递给图表。我有一个预制图表

For my case , I have a data table contains a timestamps data call "created_at" , right now , it want to get the count of the dayname of the time stamp

so ideally it will be like

enter image description here

and I like to save it on the array for me to pass the data to may chart.js chart
so what i do is

 $getdate = DB::table('watchlists')
      ->select('created_at')
      ->get();

      $getweekday = Carbon::parse($getdate)->format('l');
         $dayarray=[];
      if($getweekday =='Monday')
      {
          $mondaycount = Carbon::parse($getdate)->count();

          $dayarray=[0];
      }
      elseif($getweekday =='Tuesday')
      {
        $tuesday = Carbon::parse($getdate)->count();
        $dayarray=[1];
      }
      elseif($getweekday =='Wednesday')
      {
        $wednesday = Carbon::parse($getdate)->count();
        $dayarray=[2];
      }
      elseif($getweekday =='Thursday')
      {
        $thursday = Carbon::parse($getdate)->count();
        $dayarray=[3];
      }
      elseif($getweekday =='Friday')
      {
        $firday = Carbon::parse($getdate)->count();
        $dayarray=[4];
      }
      elseif($getweekday =='Saturday')
      {
        $saturday = Carbon::parse($getdate)->count();
        $dayarray=[5];
      }
      elseif($getweekday =='Sunday')
      {
        $sunday = Carbon::parse($getdate)->count();
        $dayarray=[6];
      }
<script>
        const ctx = document.getElementById('chart1').getContext('2d');
        const myChart = new Chart(ctx, {
            type: 'bar',
            data: {
                labels: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'],
                datasets: [{
                    label: 'Visitor Volume by weekday',
                    data: [{{ $dayarray}}],
                    backgroundColor: [
                        'rgba(255, 99, 132, 0.2)',
                        'rgba(54, 162, 235, 0.2)',
                        'rgba(255, 206, 86, 0.2)',
                        'rgba(75, 192, 192, 0.2)',
                        'rgba(153, 102, 255, 0.2)',
                        'rgba(255, 159, 64, 0.2)'
                    ],
                    borderColor: [
                        'rgba(255, 99, 132, 1)',
                        'rgba(54, 162, 235, 1)',
                        'rgba(255, 206, 86, 1)',
                        'rgba(75, 192, 192, 1)',
                        'rgba(153, 102, 255, 1)',
                        'rgba(255, 159, 64, 1)'
                    ],
                    borderWidth: 1
                }]
            },
            options: {
                scales: {
                    y: {
                        beginAtZero: true
                    }
                }
            }
        });

but when i try to run the code it gives the error of

Could not parse '[{"created_at":"2022-06-24 18:50:05"},{"created_at":"2022-06-26 13:24:42"},{"created_at":"2022-06-24 18:50:05"},{"created_at":"2022-06-26 13:24:42"},{"created_at":"2022-06-26 13:24:42"},{"created_at":"2022-06-26 13:24:42"}]': DateTime::__construct(): Failed to parse time string ([{"created_at":"2022-06-24 18:50:05"},{"created_at":"2022-06-26 13:24:42"},{"created_at":"2022-06-24 18:50:05"},{"created_at":"2022-06-26 13:24:42"},{"created_at":"2022-06-26 13:24:42"},{"created_at":"2022-06-26 13:24:42"}]) at position 0 ([): Unexpected character

I like to ask what is the error in the code , did I miss anything?

PS. I would also like to ask how to pass the data to the charts . i have a premade charts

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

猫九 2025-02-18 08:03:30

您可以为此目的使用Array redair()函数。这是一个php数组函数,您可以在此处以及 laravel Collection

$dayCounts = DB::table('watchlists')
      ->select('created_at')
      ->get()
      ->reduce(function ($car, $item) {
          $day       = Carbon::parse($item->created_at)->format('l');
          $car[$day] = ($car[$day] ?? 0) + 1;
          return $car;
      });

You can use array reduce() function for this purpose. It is a php array function that you can find docs in here and also for laravel collection

$dayCounts = DB::table('watchlists')
      ->select('created_at')
      ->get()
      ->reduce(function ($car, $item) {
          $day       = Carbon::parse($item->created_at)->format('l');
          $car[$day] = ($car[$day] ?? 0) + 1;
          return $car;
      });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文