Home > Software design >  I would like to check a SQL query of Laravel builder
I would like to check a SQL query of Laravel builder

Time:06-18

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

occured when I tried this

My laravel version 6.20

$query =  Car::where($criteria)
->get(['id'])->shuffle();

$sql = $query->toSql();

Log::info($sql);

CodePudding user response:

You could use dd or dump methods for debugging purposes. Source: https://laravel.com/docs/6.x/queries#debugging

CodePudding user response:

When you execute the query using get() it will return an instance of Illuminate\Database\Eloquent\Collection class. And there's no method named toSql() on the Collection class.

If you want to inspect the sql you should

$query =  Car::where($criteria)->select('id');

//Here $query is an instance of Illuminate\Database\Eloquent\Builder
//And it has a method named toSql
$sql = $query->toSql();

Log::info($sql);

$query->get()->shuffle();
  • Related