douzhi7451 2019-07-12 21:24
浏览 476

无法使用for循环php laravel中的whereMonth查询获取数据库结果

I want to query a database by making use of a whereMonth clause in laravel using a for loop but I am getting an empty array. If I replace $m in whereMonth clause with an integer like 7 it pulls the data.

            <table class="table table-bordered table-hover expenses-report" id="expenses-report-table">
                <thead>
                    <tr>
                        <th class="bold">Category</th>
                        <?php
                            for ($m=1; $m<=12; $m++) {
                                echo '  <th class="bold">' .date('F', mktime(0,0,0,$m,1)) . '</th>';} 
                        ?>
                        <th class="bold">year ({{\Carbon\Carbon::now()->format('Y')}})</th>
                        <?php

                        ?>
                    </tr>
                </thead>
                <tbody>
                    @foreach ($categories as $category)
                    <tr>
                        <td>
                            {{$category->name}}
                            <?php
                            for ($m = 1; $m <= 12; $m++) {
                                if (!isset($netMonthlyTotal[$m])) {
                                    $netMonthlyTotal[$m] = array();
                                }
                                $expense_results = $expenses->whereMonth('date',$m)
                                ->whereYear('date', \Carbon\Carbon::now()->format('Y'))
                                ->where('category', $category->id)
                                ->get();
                                print_r($expense_results);
                                $total_expenses = array();
                                echo '<td>';
                                foreach ($expense_results as $expense) {
                                    $expense = $expenses->where('id', $expense->id)->first();
                                    $total = $expense->amount;
                                    $totalTaxByExpense = 0;
                                    // Check if tax is applied
                                    if ($expense->tax != 0) {
                                        $totalTaxByExpense+= ($total / 100 * $expense->tax);
                                    }
                                    $taxTotal[$m][] = $totalTaxByExpense;
                                    $total_expenses[] = $total;
                                }
                                $total_expenses = array_sum($total_expenses);
                                // Add to total monthy expenses
                                array_push($netMonthlyTotal[$m], $total_expenses);
                                if (!isset($totalNetByExpenseCategory[$category->id])) {
                                    $totalNetByExpenseCategory[$category->id] = array();
                                }
                                array_push($totalNetByExpenseCategory[$category->id], $total_expenses);
                                // Output the total for this category
                                if (count($categories) <= 8) {
                                    echo $total_expenses;
                                } else {
                                    // show tooltip for the month if more the 8 categories found. becuase when listing down you wont be able to see the month
                                    echo '<span data-toggle="tooltip"
                                                                title="' . date('F', mktime(0, 0, 0, $m, 1)) . '">' . $total_expenses . '</span>';
                                }
                                echo '</td>';
                            }
                            ?>
                        </td>
                        <td class="bg-odd">
                            {{array_sum($totalNetByExpenseCategory[$category->id])}}
                        </td>
                    </tr>
                    @endforeach

                </tbody>
            </table>

I basically want to return results matching the search query including the whereMonth

  • 写回答

2条回答 默认 最新

  • doq1969 2019-07-12 21:49
    关注

    Can you replace this part

    $query->whereMonth('date', $m);
    

    with this

    $query->whereMonth('date', (string) $m);
    

    And then try again. I guess its not accepting integers.

    评论

报告相同问题?

悬赏问题

  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测