0

I'm trying to create a very complex query, I've simplified it as much as possible (2 select statements, are concatenated to the main query), the code below, works only If there are no "date" parameters sent in the request, When parameters sent, I got an error "SQLSTATE[HY000]: General error: 2031"

I think the problem is in toSql() method, but I don't know what else could I use to solve that?

Update: I need to keep leftJoin method so I can't use mergeBindings method

    // 1st select statement
    $earnQuery = DB::Table('san_charity_point_earn')->select('charity_id','earn_date',DB::raw('count(user_id) as users,sum(points) as earn_points,count(transaction_id) as earn_trans'))
    ->where(function($q) use($request){
        // filter paramters
        if($request->has("from")){
            $q->where("earn_date",">=",$request->from);
        }

        if($request->has("to")){
           $q->where("earn_date","<=",$request->to);
        }
    })
    ->groupBy('charity_id')
    ->toSql();


    // 2nd select statement
    $redeemQuery = DB::Table('san_charity_point_redeem')->select('charity_id','redeem_date',DB::raw('sum(points) as redeem_points,count(transaction_id) as redeem_trans'))
    ->where(function($q) use($request){
        // filter paramters
        if($request->has("from")){
            $q->where("redeem_date",">=",$request->from);
        }

        if($request->has("to")){
           $q->where("redeem_date","<=",$request->to);
        }
    })
    ->groupBy('charity_id')
    ->toSql();


  //full Query  
  $charities = DB::table('san_charity')->select('san_charity.charity_id','title_ar','title_en',DB::raw('
        COALESCE(users,0) as users, 
        COALESCE((earn_trans+redeem_trans),0) as transactions, 
        COALESCE(earn_points,0) as earn_points,
        COALESCE(redeem_points,0) as redeem_points'))
        ->leftJoin(DB::raw('('.$earnQuery.') earn'),function($join){
            $join->on('earn.charity_id','=','san_charity.charity_id');
        })
        ->leftJoin(DB::raw('('.$redeemQuery.') redeem'),function($join){
            $join->on('redeem.charity_id','=','san_charity.charity_id');
        })
        ->get();
6
  • In dealing with subqueries and Laravel's query builder, you'll need to remember to merge bindings from the subqueries into the "master" query. See stackoverflow.com/questions/24823915/… Commented Nov 8, 2016 at 17:03
  • @mopo922 thx for the lookup. I added another one Commented Nov 8, 2016 at 17:08
  • @mopo922, Thank you, but the questions you referred to didn't solve my problem .... I still need to keep left join and don't need to replace it with mergeBindings ... also getQuery() is undefined method!!! Commented Nov 9, 2016 at 11:22
  • @Drew, Thank you, but please check the update Commented Nov 9, 2016 at 11:29
  • mergeBindings() doesn't replace join(), it is used in partnership with it. I believe getQuery() replaces toSql(). Commented Nov 9, 2016 at 21:54

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.