laravel query builder from normal query -


db::select(db::raw( 'select a.bill_no, a.account_id, a.bill_date, a.amount_paid,                                 b.transaction_code,b.amount  bill_det left join                                 (select bill_no, transaction_code, sum(amount) amount payment_transactions                                  status = "success" group bill_no ) b                                 on a.bill_no = b.bill_no a.amount_paid != b.amount order b.bill_no')); 

this normal query.change laravel query?. tried.

$bill=db::table('bill_det')->leftjoin('payment_transactions', 'bill_det.bill_no', '=', 'payment_transactions.bill_no')                                 ->select('bill_det.bill_no','bill_det.account_id','bill_det.bill_date','bill_det.amount_paid',                                 'payment_transactions.transaction_code',db::raw('sum(payment_transactions.amount) amount'))                                 ->where('payment_transactions.status','=','success')                                 ->where('sum(payment_transactions.amount)','!=',db::raw('bill_det.amount_paid'))                                 ->groupby('bill_det.bill_no')                                 ->orderby('bill_det.bill_no','desc'); 

i can't compare -> where('sum(payment_transactions.amount)','!=',db::raw('bill_det.amount_paid'))

i used ->whereraw('bill_det.amount_paid != sum(payment_transactions.amount)')

{"error":{"type":"illuminate\database\queryexception","message":"sqlstate[hy000]: general error: 1111 invalid use of group function (sql: select count(*) aggregate (select '1' row_count bill_det left join payment_transactions on bill_det.bill_no = payment_transactions.bill_no payment_transactions.status = success , bill_det.amount_paid != sum(payment_transactions.amount) group bill_det.bill_no order bill_det.bill_no desc) count_row_table)"

db::select(db::raw( 'select a.bill_no, a.account_id, a.bill_date, a.amount_paid,                             b.transaction_code,b.amount  bill_det left join                             (select bill_no, transaction_code, sum(amount) amount payment_transactions                              status = "success" group bill_no ) b                             on a.bill_no = b.bill_no a.amount_paid != b.amount order b.bill_no')); 

after converting laravel query::

$query = \illuminate\support\facades\db::table('bill_det')             ->select('a.bill_no', 'a.account_id', 'a.bill_date', 'a.amount_paid', 'b.transaction_code', 'b.amount')             ->leftjoin(db::raw('(select bill_no, transaction_code, sum(amount) amount payment_transactions                              status = "success" group bill_no) b'), function($join) {                 $join->on('a.bill_no', '=', 'b.bill_no');             })             ->where('a.amount_paid','<>', 'b.amount')              ->orderby('b.bill_no')             ->get(); 

in case want know how use raw expression inside use this:

$query->whereraw(db::raw('(your expression!!)')); 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

Kivy: Swiping (Carousel & ScreenManager) -