【问题标题】:Convert my DB raw to Laravel Eloquent model将我的数据库原始转换为 Laravel Eloquent 模型
【发布时间】:2018-03-19 06:32:54
【问题描述】:

我有这个教程中的代码,我想知道如何将它转换为 laravel eloquent 方法,因为目前它是在 DB 原始方法中。

// $match = DiraChatLog::select(DB::raw("SUM(numberofview) as count"))
//     ->orderBy("created_at")
//     ->groupBy(DB::raw("year(created_at)"))
//     ->get()->toArray();
// $match = array_column($match, 'count');

// $missing = DiraChatLog::select(DB::raw("SUM(numberofclick) as count"))
//     ->orderBy("created_at")
//     ->groupBy(DB::raw("year(created_at)"))
//     ->get()->toArray();
// $missing = array_column($missing, 'count');

// $noAnswer = DiraChatLog::select(DB::raw("SUM(numberofclick) as count"))
//     ->orderBy("created_at")
//     ->groupBy(DB::raw("year(created_at)"))
//     ->get()->toArray();
// $noAnswer = array_column($noAnswer, 'count');

【问题讨论】:

    标签: php database laravel eloquent


    【解决方案1】:

    如果你只是想得到你的列的总和,你可以调用sum 方法而不是像这样的get

    DiraChatLog::sum('yourColumn'); // will return the only sum
    

    检查enter link description here中的聚合方法

    【讨论】:

      【解决方案2】:

      这个例子取自 laravel 文档,可以在这里找到:https://laravel.com/docs/5.6/eloquent

      $count = App\Flight::where('active', 1)->count();
      

      在本例中,App\Flight 是已经连接到表的模型。

      where方法很明显我们要获取活动列为1的数据。

      count方法也很明显,它可以让我们统计所有的数据并返回行数。

      【讨论】:

        猜你喜欢
        • 2019-04-29
        • 2016-04-27
        • 2019-05-31
        • 2019-12-15
        • 1970-01-01
        • 2019-04-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多