带while滤波器的Laravel Foreach求和结果
这是我的桌子 :科目类别图表带while滤波器的Laravel Foreach求和结果,laravel,foreach,Laravel,Foreach,这是我的桌子 :科目类别图表 身份证 名字 :会计科目表 身份证 科目类别id图表 :分类账 身份证 事务处理id 科目表 :交易 身份证 名字 日期 总数 控制器 $chart_of_accounts_category = ChartOfAccountsCategory::get(); return view ('backend.ledger_book.index', compact('chart_of_accounts_category')); 刀片/视图 @foreach ($char
身份证
名字 :会计科目表
身份证
科目类别id图表 :分类账
身份证
事务处理id
科目表 :交易
身份证
名字
日期
总数 控制器
$chart_of_accounts_category = ChartOfAccountsCategory::get();
return view ('backend.ledger_book.index', compact('chart_of_accounts_category'));
刀片/视图
@foreach ($chart_of_accounts_category as $account_category )
@foreach ($account_category->ChartOfAccounts as $item)
$item->LedgerBook->where('chart_of_accounts_id', $item->id)->sum('transaction.total'), 2)
@endforeach
@endforeach
我可以通过这个得到总数,但我仍然需要过滤更多的东西,这是我在blade中完成的。但是不能让过滤器正常工作,特别是在有符号%的地方,我会尽量减少刀片内部的查询,是否有任何地方我可以通过控制器获得结果
$item->LedgerBook->where('chart_of_accounts_id', $item->id)->where('transaction.date', 'like', '2019-06-%')->sum('transaction.total'), 2)
您可以使用
->whereYear('transaction.date','2019')->whereMonth('transaction.date','06')
另外,我建议您在控制器中使用
chartofcounts
、LedgerBook
和Transaction
。这将使您的foreach
循环更快
$chart_of_accounts_category = ChartOfAccountsCategory::with(['chartOfAccounts' => function ($query) {
$query->with(['ledgerBooks' => function ($query) {
$query->with(‘transaction’)
->whereHas(‘transaction', function ($query) {
$query->whereYear('date', '2019')
->whereMonth('date', '06');
});
}]);
}])
->get();
您可以使用
->whereYear('transaction.date','2019')->whereMonth('transaction.date','06')
另外,我建议您在控制器中使用
chartofcounts
、LedgerBook
和Transaction
。这将使您的foreach
循环更快
$chart_of_accounts_category = ChartOfAccountsCategory::with(['chartOfAccounts' => function ($query) {
$query->with(['ledgerBooks' => function ($query) {
$query->with(‘transaction’)
->whereHas(‘transaction', function ($query) {
$query->whereYear('date', '2019')
->whereMonth('date', '06');
});
}]);
}])
->get();
->whereYear('transaction.date','2019')->whereMonth('transaction.date','06')只能在控制器权限中使用?如何使用事务表进行即时加载?我怎样才能做到这一点?是的,您也可以加载事务。我更新了我的答案以显示这一点。我会在控制器中使用whereMonth和whereYear,但您可能也可以在视图中使用它们$chart\u of_accounts\u category=chartofcountscategory::with(['chartofcounts'=>function($query){$query->with(['LedgerBook'=>function($query){$query->with('Transaction')->where('date','like 2019',-06-'));}]);}])->get();看起来很有效,但在进行筛选时,它找不到合适的表。$chart\u of_accounts\u category=ChartOfAccountsCategory::with(['ChartOfAccounts'=>函数($query)use($filter\u year,$filter\u month){$query->with(['LedgerBook'=>函数($query)use($filter\u year,$filter\u month){$query->with(['Transaction'=>function($query)use($filter_year,$filter_month){$query->where('date','like','filter_year.'-'.$filter_month.'%')->where('business_id',session()->get('business_id');};}])->get();通过这个问题解决了,非常感谢mate@MohammadIskandar干得好!不客气。请注意,您可以投票并接受我的答案,以便其他可能遇到此问题的人知道这是解决方案。->whereYear('transaction.date','2019')->whereMonth('transaction.date','06'))只能在控制器中使用,对吗?如何使用事务表进行即时加载?如何实现?是的,您也可以进行即时加载事务。我更新了答案以显示这一点。我将在控制器中使用whereMonth和whereYear,但您可能也可以在视图中使用它们$chart\u of_accounts\u category=chartofcaccountscategory::with(['ChartOfAccounts'=>函数($query){$query->with(['LedgerBook'=>函数($query){$query->with('Transaction')->where('date','like','2019-06-%';}]);}]->get();似乎有效,但在进行筛选时,它找不到合适的表。$chart\u of_accounts\u category=ChartOfAccountsCategory::with(['ChartOfAccounts'=>函数($query)use($filter\u year,$filter\u month){$query->with(['LedgerBook'=>函数($query)use($filter\u year,$filter\u month){$query->with(['Transaction'=>function($query)use($filter_year,$filter_month){$query->where('date','like','filter_year.'-'.$filter_month.'%')->where('business_id',session()->get('business_id');};}])->get();通过这个问题解决了,非常感谢mate@MohammadIskandar干得好!不客气。请注意,您可以投票并接受我的答案,以便其他可能会遇到此问题的人知道这是解决方案。