【问题标题】:How to pass the value in consoletvs charts using Laravel如何使用 Laravel 在 consoletvs 图表中传递值
【发布时间】:2019-07-17 09:00:43
【问题描述】:

我想在控制台/图表中传递平均时间和用户电子邮件,我为此编写了 SQL

$currentTime = Carbon::today();

$time_difference = DB::select(DB::raw("SELECT `acu_name` ,AVG(TIMESTAMPDIFF(MINUTE, acu_at, acu_et)) as averageTime
FROM `active_user`
WHERE acu_at <= '$currentTime' GROUP BY `acu_name`"));

当我通过 foreach 方法得到这个 SQL 结果时,如果通过图表传递它就会显示错误

 $chart = Charts::create('bar', 'highcharts')
            ->title('Total Clients Average Using Time')
            ->elementLabel("Total")
            ->labels($time_difference->acu_name)
            ->values($time_difference->averageTime)
            ->responsive(false);

错误信息是:

我不明白是什么问题,请帮我找出问题所在 我附上了我的数据库结构和虚拟数据屏幕截图。 我的 php 版本是 7.3.2 Laravel 版本是 5.5

【问题讨论】:

    标签: php mysql sql database laravel


    【解决方案1】:

    当您执行DB::select() 时,它会为您提供包含表示找到的行的 stdClass 对象的数组。因此,即使您预计 DB::select() 查询会产生一行结果,结果仍会在数组中。

    所以当你做以下事情时:

    $currentTime = Carbon::today();
    
    $time_difference = DB::select(DB::raw("SELECT `acu_name` ,AVG(TIMESTAMPDIFF(MINUTE, acu_at, acu_et)) as averageTime
    FROM `active_user`
    WHERE acu_at <= '$currentTime' GROUP BY `acu_name`"));
    

    它返回一个数组,里面有匹配的记录。

    您要么需要执行 foreach() 来循环遍历 $time_difference 数组,要么执行 $time_difference[0]-&gt; acu_name

    另外,您的DB::raw 包含一个用户定义的变量,这对 SQL 注入是危险的。

    更新:

    请查看documentation,那里有很好的例子说明这样做的雄辩方式。

    【讨论】:

    • 我试过你的方法count():参数必须是数组或者实现Countable的对象(查看:F:\xampp\htdocs\mvc-panel\resources\views\home.blade.php )
    • 我添加了您正在使用的软件包的文档链接,这将对您有所帮助。您得到的错误来自使用 chsrt 包,因为它需要特定的数据结构
    【解决方案2】:

    在我看来,抛出这个特定错误确实很奇怪。但是,我们可以在您提供的代码中看到您将以下内容存储在 $time_difference 变量中:

    DB::select(...);
    

    通常这会返回一个 Illuminate\Database\Query\Builder 的实例,如果我没记错的话,它是一个对象。

    如果您查看source code,您会发现该函数确实返回$this,这是上述的一个实例。

    我可以说缺少的是查询的执行。通常select() 后跟get()first(),如documentation 所示。

    您应该采取的另一项操作是在执行查询后验证是否已返回任何内容。经过一些调整的最终代码可能如下所示:

    $time_difference = DB::table('active_user')
        ->select([
            'acu_name',
            DB::raw('AVG(TIMESTAMPDIFF(MINUTE, acu_at, acu_et)) as averageTime'),
        ])
        ->where('acu_at', '<=', $currentTime)
        ->groupBy('acu_name')
        ->first(); // Be aware this will only return the first row. Not sure if this is what you inted to do.
    
    
    if (is_null($time_difference)) {
        // Do something when it has not been found.
    }
    
    
     $chart = Charts::create('bar', 'highcharts')
        ->title('Total Clients Average Using Time')
        ->elementLabel("Total")
        ->labels($time_difference->acu_name)
        ->values($time_difference->averageTime)
        ->responsive(false);
    

    【讨论】:

    • 解析错误:语法错误,意外 ']',期待 ')' 显示此错误
    • @Jerad 这只是一个失踪)。查看代码,输入缺少的 ) 并查看它是否有效
    • @Jerad 感谢您的通知。我已经相应地更新了我的代码。
    • 之后这个错误来了 count(): Parameter must be an array or an object that implement Countable (View: F:\xampp\htdocs\mvc-panel\resources\views\home.blade .php)
    • @ThomasVanderVeen 请给出解决方案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-17
    • 2018-08-20
    • 2020-04-28
    • 2020-12-12
    相关资源
    最近更新 更多