1

Using Laravel Eloquent methods The first method to get the query of an Eloquent call is by using the toSql() method. This method returns the query without running it – good if you don't want to alter data and only get the query – but this method doesn't show the whole query if your query is more complex or if there are sub-queries.

enter code here

App\User::query()
    ->where('created_at', '<', now()->subYear())
    ->with('assignedApps', 'courses')
    ->orderBy('email', 'asc')
    ->limit(5)
    ->toSql();

1 Answer 1

2

If you want to show all the queries, then you will have to enable the query log. DB::enableQueryLog

\DB::enableQueryLog(); // enable the query log before your modal

App\User::query()
    ->where('created_at', '<', now()->subYear())
    ->with('assignedApps', 'courses')
    ->orderBy('email', 'asc')
    ->limit(5)
    ->get();

dd(DB::getQueryLog()); // after your query, dump and die get query log. 

It will return all queries result with values

Sign up to request clarification or add additional context in comments.

1 Comment

Also, It's showing how much time it'll take to execute queries. One other benefit is It'll show all the queries between enabled to get log so you can see all queries at a time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.