1

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?

1 Answer 1

2

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!

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks! Very fast response.
Thanks, Glad to see that it helps you to resolve your problem!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.