field constant using laravel query builder field constant using laravel query builder laravel laravel

field constant using laravel query builder


Using Laravel 4, and using GROUP BY, rather than ORDER BY I believe you can do something like:

$t1 = DB::table('table1')   ->select('field1',DB::raw("'FOO' as field2"))   ->groupBy('field2');$t2 = DB::table('table2')   ->select('field1',DB::raw("'BAR' as field2"))   ->groupBy('field2');$result = $t1->union($t2)->get();

I found that $t1 in this case can be an instance of Illuminate\Database\Query\Builder or Illuminate\Database\Eloquent\Builder, but the union argument ($t2) must be of type Illuminate\Database\Query\Builder.

This means that you may use eager loading with something like:

$t1 = MyTableModel::with('table3')->select...


This way, probably:

$users = DB::table('users')            ->select(DB::raw("'FOO' as field2"))            ->get();