Getting the query builder to output its raw SQL query as a String

0 votes

Hello everyone,

Here given the following code:

DB::table('users')->get();

I want to get the raw SQL query string that the database query builder above will generate. In this example, it would be SELECT * FROM users.

How can i do this?

Please help me

Thank you!!

Mar 30, 2020 in Laravel by kartik
• 37,520 points
2,397 views

1 answer to this question.

0 votes

Hello,

DB::QueryLog() only work after you execute the query $builder->get(). if you want to get the query before execute the query you can use $builder->toSql() method. this is the example how to get the sql and bind it:

    $query = str_replace(array('?'), array('\'%s\''), $builder->toSql());
    $query = vsprintf($query, $builder->getBindings());
    dump($query);

    $result = $builder->get();

or  just make your query error like calling for unexist table or column, you will see the generated query in exception XD

Hope this works!

Thank you!

answered Mar 30, 2020 by Niroj
• 82,840 points

Related Questions In Laravel

0 votes
1 answer

How to alias a table in Laravel Eloquent queries using Query Builder?

Hello @kartik, You can use less code, writing ...READ MORE

answered Nov 11, 2020 in Laravel by Niroj
• 82,840 points
10,584 views
0 votes
1 answer

What are the query builder in Laravel?

Hey, Laravel's database query builder provides a convenient, ...READ MORE

answered Mar 19, 2020 in Laravel by Niroj
• 82,840 points
1,177 views
0 votes
1 answer

How Can I Set the Default Value of a Timestamp Column to the Current Timestamp with Laravel Migrations?

Hello, To create both of the created_at and updated_at columns: $t->timestamp('created_at')->default(DB::raw('CURRENT_TIMESTAMP')); $t->timestamp('updated_at')->default(DB::raw('CURRENT_TIMESTAMP on update ...READ MORE

answered Apr 2, 2020 in Laravel by Niroj
• 82,840 points
12,144 views
0 votes
1 answer

How to pass an array as a URL parameter?

Hello, Here you can try this out var myArray ...READ MORE

answered Apr 14, 2020 in Laravel by Niroj
• 82,840 points
37,963 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
23,082 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18, 2020 in Laravel by Niroj
• 82,840 points
3,017 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
2,949 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23, 2020 in Laravel by Niroj
• 82,840 points
44,889 views
0 votes
1 answer

How to get current path of a Request with its query parameters?

Hello @kartik, Try to use the following: \Request::getRequestUri() Hope this ...READ MORE

answered Aug 11, 2020 in Laravel by Niroj
• 82,840 points
2,041 views
0 votes
1 answer

How to get a variable name as a string in PHP?

Hello @kartik, You can use this: function varName( $v ...READ MORE

answered Sep 29, 2020 in Laravel by Niroj
• 82,840 points
2,520 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP