Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

fico7489's avatar

Laravel/PHP variables in same functions are somehow tied to session?

What is happening here, I have laravel code :

class HomeController extends Controller
{
    public function index()
    {
        $this->test();
    }

    public function index2()
    {
        $this->test();
    }

    private function test(){
        $milliseconds = round(microtime(true) * 1000);
        \DB::transaction(function (){
            $order = Order::find(1)->lockForUpdate()->first();
            $order->update(['session_id' => str_random(40)]);
            sleep(2);
        });
        $milliseconds2 = round(microtime(true) * 1000);
        echo 'milliseconds=' . ($milliseconds2 - $milliseconds) . ' ms';
    }
}

when I run index route in two tabs in browser results is:

first tab  : milliseconds=2068 ms
second tab : milliseconds=2065 ms

although one request takes about 2 seconds, another 4 seconds

when I run index route in one tab, index2 in second tab result is something like:

first tab  : milliseconds=2082 ms
second tab : milliseconds=4117 ms

also one request takes about 2 seconds, another 4 seconds. This is very strange, what is going on here ?!??

0 likes
1 reply
EventFellows's avatar

You are using sleep(2); so I would guess that you are executing your function twice.

also you use http://php.net/manual/de/function.microtime.php which return microseconds so you might want to **devide by 1000** and not multiply by 1000.

And Order::find(1)->lockForUpdate()->first() is wrong syntax. For find() you don't need any first() so you might want to check if that works as you intend.

Please or to participate in this conversation.