mardi 3 novembre 2015

Laravel 5: Query Builder creates bad SQL syntax

I am trying to create a dynamic query by using checkbox selections of a form and i am using Laravel 5 query builder and here is the the sql that i get by using toSql() method after i got it when the form processed.

select `v`.`id ,v`.`model` from `vehicles` as `v`

The problem is query causes SQL syntax error since it is missing some quotes. And below is how i create the query

            $query = 'v.id';

            if($model == 1){
                $query .= ' ,v.model';
            }

            if($marka == 1){
                $query .= ' ,v.brand_name';
            }

            $query_result = DB::table('vehicles AS v')->select($query)->toSql();

How do i create a proper sql query ? Any help would be appreciated.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire