Why entire table is locked instead of a single row?

Laravel Version: 7.16

PHP Version: 7.4.6

Database Driver & Version: MariaDB 10.4.13 InnoDb engine

Below laravel(php) code is going to return array response Once action method called for id=1 it takes 20 sec to get committed. Meanwhile the same method called for id=2 response waits until the first transaction is getting finished. why row-level locking is not working.

protected function actions($  id)     {       DB::beginTransaction();       try {           if($  id==1)             sleep(20);            $  invoice = Invoice::lockForUpdate()->find($  id);           $  name = 'Approve';           if ($  invoice->approved) {              $  name = 'Disapprove';                   }          } catch (Exception $  e) {             DB::rollBack();           }       DB::commit();          return [             'name' => $  name,             'id' => 'menu-approve',             'data-status' => $  invoice->approved         ];     } 

A lockForUpdate() locks a specific row until it was committed successfully (Select * from invoices where id=1 FOR UPDATE). But select * from invoices where id=2 FOR UPDATE should not be wait until the first query get committed. Also without specifying a lock ( Select * from invoices) records are not drawn and it waits the first transaction to be committed. What could be the reason that entire table is locked instead of a single row?