0

I know this question has already been asked here : Laravel Query Scope in Loop

But I am not able to resolve the issue.

    foreach ($activePortalProviders as $eachProvider) {
        $stockModel = new Stock();

        $response = array();
        $stockList = array();

        $portalProviderID = $eachProvider['portalProviderPID'];
        $getAllStocks = $stockModel->getAllStockBaseOnProviderID($portalProviderID)->select($selectedColumn)->get();

        if (count($getAllStocks) > 0) {

            foreach ($getAllStocks as $eachStock) {
                $gameModel = new Game();

                            //To find the game related to that particular stock and provider id
                $gameStatus = [1, 2]; // open/closed games should be listed
                $currentTimeStamp = microtimeToDateTime(getCurrentTimeStamp());
                $portalProviderPID = $eachStock['portalProviderPID'];
                DB::connection()->enableQueryLog();

                $queries = null;
                Log::debug('--before--'. microtimeToDateTime(getCurrentTimeStamp(), true));
                $gameData = $gameModel->getAllProviderGamesByStock($portalProviderPID, $eachStock['stockUUID'], $gameStatus, $currentTimeStamp);
                $queries = DB::getQueryLog();
                Log::debug($queries);
                Log::debug('--after--'. microtimeToDateTime(getCurrentTimeStamp(), true));

                //other code..........

                unset($gameModel);

            }
            //other code..........
        }
        unset($stockModel);
    } 

tried creating individual objects and unsettling them

tried using (clone $gameModel)->getAllProviderGamesByStock(---Params--)

but the query log I get, keeps adding the old query each time like:

[2020-04-15 12:03:46] local.DEBUG: --before--2020-04-15 12:03:46.2226  
[2020-04-15 12:03:46] local.DEBUG: array (
  0 => 
  array (
    'query' => '---qry--',
    'bindings' => 
    array (
      0 => 'active',
      1 => 2,
      2 => '7b6c89f3-fcef-47b3-b4d4-ce550e0b56ed',
      3 => 1,
      4 => 2,
    ),
    'time' => 2.22,
  ),
)  
[2020-04-15 12:03:46] local.DEBUG: --after--2020-04-15 12:03:46.2403  
[2020-04-15 12:03:46] local.DEBUG: --before--2020-04-15 12:03:46.2412  
[2020-04-15 12:03:46] local.DEBUG: array (
  0 => 
  array (
    'query' => '---qry--',
    'bindings' => 
    array (
      0 => 'active',
      1 => 2,
      2 => '7b6c89f3-fcef-47b3-b4d4-ce550e0b56ed',
      3 => 1,
      4 => 2,
    ),
    'time' => 2.22,
  ),
  1 => 
  array (
    'query' => '---qry--',
    'bindings' => 
    array (
      0 => 'active',
      1 => 2,
      2 => 'ebac522c-24ae-4ba5-8bc9-1c32641ab08f',
      3 => 1,
      4 => 2,
    ),
    'time' => 1.83,
  ),
)  
[2020-04-15 12:03:46] local.DEBUG: --after--2020-04-15 12:03:46.2454  
[2020-04-15 12:03:46] local.DEBUG: --before--2020-04-15 12:03:46.2462  
[2020-04-15 12:03:46] local.DEBUG: array (
  0 => 
  array (
    'query' => '---qry--',
    'bindings' => 
    array (
      0 => 'active',
      1 => 2,
      2 => '7b6c89f3-fcef-47b3-b4d4-ce550e0b56ed',
      3 => 1,
      4 => 2,
    ),
    'time' => 2.22,
  ),
  1 => 
  array (
    'query' => '---qry--',
    'bindings' => 
    array (
      0 => 'active',
      1 => 2,
      2 => 'ebac522c-24ae-4ba5-8bc9-1c32641ab08f',
      3 => 1,
      4 => 2,
    ),
    'time' => 1.83,
  ),
  2 => 
  array (
    'query' => '---qry--',
    'bindings' => 
    array (
      0 => 'active',
      1 => 2,
      2 => '01fbd201-2837-4589-b60d-7321f319cd72',
      3 => 1,
      4 => 2,
    ),
    'time' => 1.82,
  ),
)  
[2020-04-15 12:03:46] local.DEBUG: --after--2020-04-15 12:03:46.2503  

and this keeps growing.

Is anyone else facing similar issue?

1 Answer 1

0

Seems to me like your functional code is fine, but the way you are using the query log is misleading yourself because you never flushed the log

Try flushing the querylog like so:

...
$queries = DB::getQueryLog();
DB::flushQueryLog();
...
Sign up to request clarification or add additional context in comments.

1 Comment

LOL, Thank you so much, I wasted plenty of my time on this :D

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.