Php / Laravel seems to be running sync code in parallel unexpectedy

This is my first approach to Laravel (5.4) Something really strange is happening, I've my code structured like this

$ko = 0;
foreach($items as $item)
{
    $ok = $item->laravelQuery();
    if(!$ok)
    {
        $ko++;
    }
}
//LARAVEL INSERTS / UPDATES NOT OVER ITEMS
if($ko == 0) //(1)
{
    //LARAVEL INSERTS / UPDATES OVER ITEMS
}
//LARAVEL INSERTS / UPDATES NOT OVER ITEMS
if($ko == 0) //(2)
{
    //LARAVEL INSERTS / UPDATES OVER ITEMS
}

It is obviously expected to run everything synchronously, but after having unexpected results I've placed a log in every line. Log shows that code into if's (1) and (2) is running at the same time that the code into the foreach, which alters items making $ko to increase. So I ended having $ko > 0 and the code which relies on $ko = 0 being executed....

I don't know if I'm missing something or if this is expected to work this way by design on some twisted (?) way, but I appreciate your help