I am getting an error

Object of class Illuminate\Database\Query\Expression could not be converted to string for following code

$draft=$this->query->whereRaw('t.id IN '.\DB::raw("({$inner_query_draft->toSql()})"))->mergeBindings($inner_query_draft);

I have tried ddSql and dump but still the same issue

2

There are 2 answers

1
Toby Allen On

Should

$draft=$this->query->whereRaw

be

$draft=$this->query()->whereRaw

note the query() brackets.

0
Skeets On

I'm guessing this is an error you ran into while upgrading to Laravel 10 from 9?

The result of DB::raw() can no longer be cast as a string like that:

https://laravel.com/docs/10.x/upgrade#database-expressions

So the correct line would be:

$draft=$this->query()->whereRaw('t.id IN '.\DB::raw("({$inner_query_draft->toSql()})")->getValue(DB::connection()->getQueryGrammar()))->mergeBindings($inner_query_draft);