我想通过使用for循环使用laravel中的whereMonth子句来查询数据库,但是我得到了一个空数组。如果我将whereMonth子句中的$ m替换为类似7的整数,则会提取数据。
<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>
我基本上想返回与搜索查询匹配的结果,包括whereMonth
答案 0 :(得分:0)
您可以替换这部分吗
Configures a MySQL DB instance to be a Read Replica of an instance of MySQL running external to Amazon RDS
与此
$query->whereMonth('date', $m);
然后重试。我猜它不接受整数。
答案 1 :(得分:0)
我能够通过使用Raw查询使其工作
$ rawQuery =“从tblexpenses
处选择金额,标识号,税款,MONTH(date)= $ m AND YEAR(CURRENT_DATE)AND category = $ category-> id GROUP BY id”;
$ expense_results = DB :: select(DB :: raw($ rawQuery));
不过,如果有人可以帮助将该查询翻译为laravel,我将不胜感激