Home > database >  Laravel query builder not returning the correct sql statement, the strings look like variables
Laravel query builder not returning the correct sql statement, the strings look like variables

Time:09-26

I have the following Laravel query, it does not appear to be returning the $abort and $pend variables as strings:

$abort = "Aborted";
$pend = "Pending";
$transactions = DB::table('callpay_transactions')
->select(DB::raw('SUBSTR(created,1,7) as YrMth') ,DB::raw('SUM(amount) as total_sales'  ))
->where('status', '!=', $abort)
->where('status', '!=', $pend)
->groupBy(DB::raw('SUBSTR(created,1,7)'))
    ->toSql();
    
dd($transactions);

The return sql query is as follows

"select SUBSTR(created,1,7) as YrMth, SUM(amount) as total_sales from `callpay_transactions` where `status` != ? and `status` != ? group by SUBSTR(created,1,7)"
        

Any ideas as to why my $abort and $pend variables show as strings?

CodePudding user response:

i thing that is because of prepared statment and ? also indicate a placeholder while using prepared statment in php. instead of to sql if you us get you will see the result.

CodePudding user response:

If you want to see the full output with your parameters in place, you can do the following:

use Illuminate\Support\Str;

$abort = "Aborted";
$pend = "Pending";

$transactions = DB::table('callpay_transactions')
->select(DB::raw('SUBSTR(created,1,7) as YrMth') ,DB::raw('SUM(amount) as total_sales'  ))
->where('status', '!=', $abort)
->where('status', '!=', $pend)
->groupBy(DB::raw('SUBSTR(created,1,7)'));
    
dd(Str::replaceArray('?', $transactions->getBindings(), $transactions->toSql()));

That will replace the ? characters with their actual binding values from your query.

  • Related