Php Laravel |比较两个集合,并根据其创建的_at和删除的_at字段回显它们的属性
我想显示贷款管理laravel应用程序的活动列表 因此,可以在一天内发放贷款(Php Laravel |比较两个集合,并根据其创建的_at和删除的_at字段回显它们的属性,php,laravel,sorting,merge,laravel-5.4,Php,Laravel,Sorting,Merge,Laravel 5.4,我想显示贷款管理laravel应用程序的活动列表 因此,可以在一天内发放贷款(在处创建),或者关闭/完成贷款(在处删除)。 我使用以下方法获取这两个集合: $new_loans = loan::withTrashed()->where('created_at', '>=', Carbon::today())->get(); $completed_loans = loan::onlyTrashed()->where('deleted_at', '>=', Carbo
在
处创建),或者关闭/完成贷款(在
处删除)。
我使用以下方法获取这两个集合:
$new_loans = loan::withTrashed()->where('created_at', '>=', Carbon::today())->get();
$completed_loans = loan::onlyTrashed()->where('deleted_at', '>=', Carbon::today())->get();
我的问题是,我希望根据贷款创建时间和贷款关闭时间动态显示这两个集合的属性
假设:
new_loans | completed_loans
1 - 10 am | 3 - 8 am
2 - 12 am | 4- 11 am
输出应为:
活动:
- 上午3点至8点,贷款结束
- 上午1-10点,新贷款
- 上午4点至11点,贷款结束
- 凌晨2-12点,新贷款
foreach
和ifs
编写一些逻辑,但显然是错误的
foreach ($new_loans as $new)
{
foreach ($completed_loans as $comp)
{
if($comp->deleted_at > $new->created_at)
{
echo $new->customer->first_name.'----- New---'.$new->created_at->diffForHumans()."<br>";
}
else
{
echo $comp->customer->first_name.'----- Completed---'.$comp->deleted_at->diffForHumans()."<br>";
}
}
}
foreach($new_贷款为$new)
{
foreach(已完成的贷款为$comp)
{
如果($comp->deleted\u at>$new->created\u at)
{
echo$new->customer->first_name.----new-->.$new->created_at->diffForHumans()。“
”;
}
其他的
{
echo$comp->customer->first_name.----已完成---.$comp->deleted_at->diffForHumans()。“
”;
}
}
}
有人能告诉我怎么做吗?您可以将整个内容合并到一个查询中,然后按照所需的顺序对结果进行排序 您还应该是
customer
关系,这样您就不会对每个循环执行新的查询
所有这些加在一起看起来像:
$loans = loan::with('customer')
->withTrashed()
->where('created_at', '>=', Carbon::today())
->orWhere(function($query) {
return $query->whereNotNull('deleted_at')
->where('deleted_at', '>=', Carbon::today());
})
->get()
->sortBy(function($loan) {
return $loan->deleted_at === null ? $loan->created_at : $loan->deleted_at;
});
foreach ($loans as $loan) {
if($loan->deleted_at === null) {
echo $loan->customer->first_name.'----- New---'.$loan->created_at->diffForHumans()."<br>";
} else {
echo $loan->customer->first_name.'----- Completed---'.$loan->deleted_at->diffForHumans()."<br>";
}
}
$loans=loan::with('customer'))
->withTrashed()
->其中('created_at','>=',Carbon::today())
->orWhere(函数($query){
返回$query->whereNotNull('deleted_at')
->其中('deleted_at','>=',Carbon::today());
})
->得到()
->sortBy(功能($loan){
返回$loan->deleted_at==null?$loan->created_at:$loan->deleted_at;
});
foreach($贷款作为$贷款){
如果($loan->deleted_at==null){
echo$loan->customer->first_name.----新建---.$loan->created_at->diffForHumans()。“
”;
}否则{
echo$loan->customer->first_name.----已完成--.$loan->deleted_at->diffForHumans()。“
”;
}
}
您可以将整个内容组合到一个查询中,然后根据所需顺序对结果进行排序
您还应该是customer
关系,这样您就不会对每个循环执行新的查询
所有这些加在一起看起来像:
$loans = loan::with('customer')
->withTrashed()
->where('created_at', '>=', Carbon::today())
->orWhere(function($query) {
return $query->whereNotNull('deleted_at')
->where('deleted_at', '>=', Carbon::today());
})
->get()
->sortBy(function($loan) {
return $loan->deleted_at === null ? $loan->created_at : $loan->deleted_at;
});
foreach ($loans as $loan) {
if($loan->deleted_at === null) {
echo $loan->customer->first_name.'----- New---'.$loan->created_at->diffForHumans()."<br>";
} else {
echo $loan->customer->first_name.'----- Completed---'.$loan->deleted_at->diffForHumans()."<br>";
}
}
$loans=loan::with('customer'))
->withTrashed()
->其中('created_at','>=',Carbon::today())
->orWhere(函数($query){
返回$query->whereNotNull('deleted_at')
->其中('deleted_at','>=',Carbon::today());
})
->得到()
->sortBy(功能($loan){
返回$loan->deleted_at==null?$loan->created_at:$loan->deleted_at;
});
foreach($贷款作为$贷款){
如果($loan->deleted_at==null){
echo$loan->customer->first_name.----新建---.$loan->created_at->diffForHumans()。“
”;
}否则{
echo$loan->customer->first_name.----已完成--.$loan->deleted_at->diffForHumans()。“
”;
}
}
非常好,但我认为这个查询有一个问题,每当贷款完成(删除)时,结果中也会出现相应的创建通知。例如,如果贷款完成,我会得到输出,如Ronak------New---3天前Ronak------completed---4秒前
,我只需要当天发生的事情我已经更新了答案,因此它只会像您的示例中那样重复一个或另一个。很好,但是这个查询有一个问题,我认为,每当贷款完成(删除)时,结果中也会出现相应的创建通知。例如,如果贷款完成,我得到的输出像Ronak------New---3天前Ronak------Completed---4秒前
,我只需要当前一天发生的事情我已经更新了答案,所以它只会像你的例子中那样重复一个或另一个