Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

BMS51's avatar
Level 1

Laravel 10 DB::RAW confusion. (docs)

Hello all,

Currently i'm preparing my project to move from Laravel 9.x to 10.x Yet i'm confused with the update process.

Database "expressions" (typically generated via DB::raw) have been rewritten in Laravel 10.x to offer additional functionality in the future. Notably, the grammar's raw string value must now be retrieved via the expression's getValue(Grammar $grammar) method. Casting an expression to a string using (string) is no longer supported.

Yet in the documentation it still is present. When i don't change anything in my code the queries where i still use DB::Raw() still work in Laravel 10.3.3

example:

return $query->where('id', '=', $listId)
            ->with(['dynamicQuestions' => function ($x) use ($lang) {
                $x->with(['latestTranslation' => function ($y) use ($lang) {
                    $y->where('language', '=', $lang)
->select('dynamic_questions_translations.id', 'dynamic_question_id', 'translation');
                }])
->select('dynamic_questions.id', 'default_value as question', DB::Raw("IF(both_answers_valid = true, '0,1', `correct_answer`) as correct_answer") );
            }])
            ->select('id', 'name');
->select(DB::Raw("concat(dynamic_question,'|',dynamic_question_correct_answer) as dynamic_question"), 'dynamic_question_answer', $tableName.'.user_location as start_location',
                DB::Raw("IFNULL(original_client_value) as client_name") ,
                DB::Raw("IFNULL(original_project_value) as project_name"),
            )
```
What am I missing? Does it still work when used inside Queries etc? I'm kinda LOST sorry :/
0 likes
4 replies
LaryAI's avatar
Level 58

The Laravel 10 documentation states that casting an expression to a string using (string) is no longer supported. However, it seems that the DB::raw() method still works as expected in queries.

If you want to follow the updated syntax, you can retrieve the raw string value of an expression using the getValue() method. Here's an example:

$expression = DB::raw('CONCAT(first_name, " ", last_name)');
$value = $expression->getValue(DB::getQueryGrammar());

In this example, $value will contain the raw string value of the expression, which is 'CONCAT(first_name, " ", last_name)'.

So, if you want to update your code to use the new syntax, you can replace DB::Raw() with DB::raw() and retrieve the raw string value using the getValue() method. However, if your existing code is working fine, you can continue to use it as is.

4 likes
BMS51's avatar
Level 1

So i can probably continue to use the DB::Raw() function anyway

1 like
jlrdw's avatar
jlrdw
Best Answer
Level 75

@BMS51 if you aren't casting to string.

1 like
BMS51's avatar
Level 1

@jlrdw thanks, yeah it had to sink in a bit .... i think i paniced a bit since i have large queries that rely on some DB::Raw() but they are not cast with (string) or using __toString.

Ran my tests after upgrading to 10.3 and they all passed so i'm good i guess :)

Please or to participate in this conversation.