1

I am trying to set a column result literal with the Laravel query builder. By writing raw SQL I would achieve this with:

SELECT
  `field1`, 
  `field2`, 
  'Test' AS `field3` 
FROM `Test`;

Therefore MySQL will always return Test for column field3. I'm trying to do this with the query builder like select('field1', 'field2', '"Test" AS field3') but this doesn't seem to work. It will return an error that Test is not a column name.

Does anyone have an idea how I could do this?

2
  • Didn't you mean field3 AS Test? Commented Oct 21, 2014 at 23:44
  • @Raphael_ No, he wants the string Test to be always the value of field3 (which might not even exist). Commented Oct 22, 2014 at 0:21

1 Answer 1

8

You want the DB::raw() function:

->select('field1', 'field2', DB::raw("'Test' AS field3"))
Sign up to request clarification or add additional context in comments.

Comments

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.