mardi 26 juin 2018

How to get bindings with datatype using Laravel 5.*

i'm trying to make a subquery something like

SELECT COUNT (*) as total_count FROM ($sql) as count_table

here the method:

public function getTotal($query = null)
    {
        if (is_null($query)) {
            return $this->total;
        }

        $sql = $query->toSql();

        $bindings = $query->getBindings();

        foreach ($bindings as $binding) {
            $bind = gettype($binding) === '' ? "'" . $binding . "'" : $binding; //is not working as expected
            $sql = preg_replace('/\?/', $bind, $sql, 1);
        }

        dd($sql);

        $sql = str_replace('\\', '\\\\', $sql);

        $total = \DB::connection('sqlsrv')->select(\DB::raw("select count(*) as total_count from ($sql) as count_table"));

        return $total[0]->total_count;
    }

the problem is when i preg_replace all ? in the sql query i get only the values without the datatype.

my getBindings():

array:14 [▼
  0 => 0
  1 => "D"
  2 => 0
  3 => 0
  4 => "CZ"
  5 => "00000"
  6 => "15000"
  7 => "D"
  8 => 0
  9 => 0
  10 => 0
  11 => "AT"
  12 => "0000"
  13 => "2000"
]

in this array i have some integers and strings and i would like to bind exact the same datatypes.

i have tried to check the strings with gettype() but this is not working.



from Newest questions tagged laravel-5 - Stack Overflow https://ift.tt/2N0FvBa
via IFTTT

Aucun commentaire:

Enregistrer un commentaire