Debugging query in laravel
- Steps for Logging query in Log file
- First use DB;
- DB::enableQueryLog();
- you can dd(DB::getQueryLog())
- To get last executed query use - end(DB::getQueryLog())
DB::enableQueryLog(); // Run your queries // ... // Then to retrieve everything since you enabled the logging: $queries = DB::getQueryLog(); foreach($queries as $i=>$query) { Log::debug("Query $i: " . json_encode($query)); } Log::debug('Last query is :'.$last_query = end($queries););
In next post we will learn more about relationship methods
- with()
- has()
- whereHas()
- withCount()