It may sound as a duplicate question. Please I have not found a working solution.I want to add additional field to my query in laravel but I am getting error. This is the php implementation
select id, "extra_column as type" from cases
have tried
DB::table('cases')
->select(['id'])
->selectSub(function($query){
$query->selectRaw('extra_column');
},'type')
->get();
but I keep getting error
CodePudding user response:
You need to use like this:
DB::table('cases')
->select(['id', 'extra_column as type'])
->get();
CodePudding user response:
Remove square brackets from the select statement
add a new column string type with , Your code like this type
DB::table('cases') ->select('id', 'column_name') ->selectSub(function($query){ $query->selectRaw('extra_column'); },'type') ->get();
CodePudding user response:
Please try addSelect('extra_column as type')
.
DB::table('cases')
->select(['id'])
->addSelect(`<additional column>`) // We can able to use variable as well $value = 'extra_column as type';
->get();
CodePudding user response:
I later found out the solution. the extra_column was missing a double quote
DB::table('cases')
->select(['id'])
->selectSub(function($query){
$query->selectRaw('"extra_column"');
},'type')
->get();