To get the SQL query from the query builder in Laravel as string, you can use the toSql() method on the query builder instance.
For Example: you want to get the raw SQL query for the following.
DB::table('users')->toSql();
// This will return 'select * from users;'
DB::table('users')->where('role', 1)->toSql();
// select * from users where role = ?
// Note that the above returned only the raw parameters.
// To get the value bindings you need to use the method getBindings();
DB:table('users')->where('role', 1)->getBindings();
// This will return `[ 1 ]`