Laravel 5 如何使用Laravel'对两个不同表中的两列的值求和;他口若悬河
我正在使用jQuery datatables插件,并使用foreach循环添加列valores_receber(values_to_receive),该循环进行求和,但以一种单独的方式进行,如下面代码中所示Laravel 5 如何使用Laravel'对两个不同表中的两列的值求和;他口若悬河,laravel-5,datatables,eloquent,Laravel 5,Datatables,Eloquent,我正在使用jQuery datatables插件,并使用foreach循环添加列valores_receber(values_to_receive),该循环进行求和,但以一种单独的方式进行,如下面代码中所示 public function anyDataReceber() { $clientes = Partner::whereHas('faturamentos', function($q){ $q->where('status', '<>', 'CANCEL
public function anyDataReceber() {
$clientes = Partner::whereHas('faturamentos', function($q){
$q->where('status', '<>', 'CANCELADO');
})
->orWhereHas('faturamentosLivres', function ($q) {
$q->where('status','<>','CANCELADO');
})
->with('faturamentos')
->with('faturamentosLivres');
return Datatables::of($clientes)
->addColumn('valores_receber', function ($clientes) {
$total = 0;
foreach($clientes->faturamentos as $fatura1) {
if ($fatura1->status != 'CANCELADO') $total += $fatura1->total_usd - $fatura1->valor_pago;
}
foreach($clientes->faturamentosLivres as $fatura2) {
if ($fatura2->status != 'CANCELADO') $total += $fatura2->total_usd - $fatura2->valor_pago;
}
return number_format($total,2,',','.');
})
->addColumn('action', function ($clientes) {
$actions = '';
$actions = '<a href="/admin/financeiro-matriz/contas-receber/'.$clientes->id.'" class="btn btn-xs btn-primary"><i class="fa fa-eye"></i> Visualizar</a>';
return $actions;
})
->make(true);
公共函数anyDataReceber(){
$clientes=Partner::whereHas('faturamentos',function($q){
$q->where('status','CANCELADO');
})
->或wherehas('faturamentosLivres',函数($q){
$q->where('status','CANCELADO');
})
->带('faturamentos')
->带有('faturamentosLivres');
返回数据表::of($clientes)
->addColumn('valores_receber',函数($clientes){
$total=0;
foreach($clientes->faturamentos as$fatura1){
如果($fatura1->status!=“CANCELADO”)$total+=$fatura1->total\u usd-$fatura1->valor\u pago;
}
foreach($clientes->faturamentosLivres as$fatura2){
如果($fatura2->status!=“CANCELADO”)$total+=$fatura2->total\u usd-$fatura2->valor\u pago;
}
返回编号_格式($total,2,',',');
})
->addColumn('action',函数($clientes){
$actions='';
$actions='';
返回$actions;
})
->使(真实);
}
这里的问题是,datatable无法对列valores_receber排序,因为该列在来自Eloquent的结果查询中不存在
我研究了mySQL中的SUM()函数,但我无法使用Elount和表之间的关系来解决问题
我检查了以下答案,这些答案应该是正确的,但是使用的是普通的SQL,根据我的研究,它需要某种join或union语句,但是如何在雄辩的语言中执行呢
- 使用雄辩的语句进行查询,该语句对表faturamentos和表faturamentos_livres(发票和自由发票)中的列total_usd的值求和
- 这些表需要受status列的限制,status列必须是除CANCELADO(已取消)之外的任何值。此状态列是枚举类型
公共函数anyDataReceber(){
//1-获取具有faturamentos或faturamentosLivres的客户端的所有ID
$clientes=Partner::whereHas('faturamentos',function($q){
$q->where('status','CANCELADO');
})
->或wherehas('faturamentosLivres',函数($q){
$q->where('status','CANCELADO');
})
->带('faturamentos')
->带('faturamentosLivres')
->拔毛('id')
->toArray();
//2-转换为字符串以在查询中使用
$ids=内爆(“,”,$clientes);
//3-我想使用Elount进行的丑陋查询。它根据状态进行过滤,并根据之前返回的客户端ID进行过滤。它也没有吸引力,因为它使用了子选择和连接
$result=DB::select(DB::raw(
'选择partners.client,cliente_id,(总和(t.total_usd)-SUM(t.valor_pago))作为valores_receber
FROM(从faturamentos中选择客户id、total_usd、valor_pago,其中状态为“CANCELADO”,客户id为(“.$id”。)
联合所有
从faturamentos_livres中选择客户id、总美元、valor_pago,其中状态为“CANCELADO”,客户id为(“.$id”))
加入partners上的合作伙伴。id=客户\ u id
按客户编号分组'
));
//4-转换为集合,因为Datatables类需要它
$result=收集($result);
//5-返回是唯一一个容易阅读的部分,感觉它在正确的轨道上
返回数据表::of($result)
->editColumn('valores\u receber',函数($result){
返回编号格式($result->valores_receber,2',',');
})
->addColumn('action',函数($result){
$actions='';
$actions='';
返回$actions;
})
->使(真实);
}
如果您知道如何使用mysql,我会使用原始表达式
看来,只有雄辩才能做到这一点。我将尝试使用原始表达式并共享结果
public function anyDataReceber() {
// 1 - getting all IDs of clients that have faturamentos or faturamentosLivres
$clientes = Partner::whereHas('faturamentos', function($q){
$q->where('status', '<>', 'CANCELADO');
})
->orWhereHas('faturamentosLivres', function ($q) {
$q->where('status','<>','CANCELADO');
})
->with('faturamentos')
->with('faturamentosLivres')
->pluck('id')
->toArray();
// 2 - Converting to string for use in the query
$ids = implode(',',$clientes);
// 3 - the ugly query that I want to do using Eloquent. It filters by the status and filters by clients´ ids returned before. It is not appealing either because it uses subselects and a join
$result = DB::select(DB::raw(
'SELECT partners.client, cliente_id, (SUM(t.total_usd) - SUM(t.valor_pago)) AS valores_receber
FROM (SELECT cliente_id, total_usd, valor_pago FROM faturamentos WHERE status <> "CANCELADO" AND cliente_id IN ('.$ids.')
UNION ALL
SELECT cliente_id, total_usd, valor_pago FROM faturamentos_livres WHERE status <> "CANCELADO" AND cliente_id IN ('.$ids.')) t
JOIN partners ON partners.id= cliente_id
GROUP BY cliente_id'
));
// 4 - converting to Collection because the Datatables class expects it
$result = collect($result);
// 5 - The return is the only part that is easy to read and feels like it is in the right track
return Datatables::of($result)
->editColumn('valores_receber', function ($result) {
return number_format($result->valores_receber,2,',','.');
})
->addColumn('action', function ($result) {
$actions = '';
$actions = '<a href="/admin/financeiro-matriz/contas-receber/'.$result->cliente_id.'" class="btn btn-xs btn-primary"><i class="fa fa-eye"></i> Visualizar</a>';
return $actions;
})
->make(true);
}