Nere
Nere

Reputation: 4097

Add custom field during select mysql in laravel query

I have this query in laravel 5.2

$obj_custom_stdy_data = QstCustomStudyData::where('student_id', $this->data_user['student_id'])
                    ->select($list_id . ' as list_id ', 'chapter_id', 'subject_id', 'subject_code_id')
                    ->get()
                    ->toArray();

Well I have a fixed value $list_id got from top code. Actually I want to add new field during query selection as list_id. However I got error for such that method.

When I tried in mysql IDE for example:

SELECT (1+2) as total, c.* FROM users

Then the result is no wrong at all.

Is that anyway to write in query builder for laravel instead of raw style?

Upvotes: 1

Views: 1627

Answers (1)

Saumya Rastogi
Saumya Rastogi

Reputation: 13703

You can take the use of DB::raw() method of QueryBuilder like this:

->select(DB::raw('(1+2) as total'));

See more about Query Builder's Raw Expressions

Hope this helps!

Upvotes: 2

Related Questions