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

davy_yg's avatar
Level 27

using toSql()

Method Illuminate\Database\Eloquent\Collection::toSql does not exist

 $range = ProductCategory::whereIn('id', $categoryIds)
                        ->translatedIn('en')->get();      

 dd($range->toSql());

I want to see the result of the sql but unable to do so. Even after I delete the ->get(); still does not works.

ref: http://mydoubts.in/blog/tag/tosql-not-working-in-laravel/

Any clue why?

0 likes
5 replies
tisuchi's avatar

@davy_yg

try this-

ProductCategory::whereIn('id', $categoryIds)
    ->translatedIn('en')->toSql();
1 like
Tray2's avatar

The range is a collection of the result returned from the query. You need to do the toSqlon the query.

So try

dd( ProductCategory::whereIn('id', $categoryIds)
                        ->translatedIn('en')->toSql()); 
2 likes
kingsconsult's avatar

This is old, but you forgot to add query(), so it will be

$range = ProductCategory::query()->whereIn('id', $categoryIds) ->translatedIn('en') ;

dd($range->toSql());

JussiMannisto's avatar

@Snapey Also instead of:

dd(...->translatedIn('en')->toSql());

You can chain the dd() call:

...->translatedIn('en')->dd();

Don't know if this is common knowledge, I just recently found out myself.

Please or to participate in this conversation.