vendredi 7 octobre 2016

Use DB::raw() with Table prefix

I would to make a SUM in query with Laravel 5 Query Builder.

return DB::table('table1')->join('table2','table1.id','=','table2.id')
            ->where('table1.user_id','=',$userId)
            ->whereMonth('table2.date', '=', $month )
            ->whereYear('table2.date', '=', $year )->select('table2.*', DB::raw('SUM(table1.count) AS count_single'))->groupby('table2.id')->get();

but my problem is that I have a prefix table (xc_), and DB::raw return error

"Column not found: 1054 Unknown column 'table1.count' "

It is a problem with prefix table, because if I put:

$table_prefix = env('DB_TABLE_PREFIX', 'xc_');
DB::raw('SUM('.$table_prefix.'table1.count) AS count_single')

It work, so the problem is prefix, but I don't like this method, and so: there is a method for use DB::Raw without specifying prefix table?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire