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

Loomix's avatar

Positive query result but no database update

I want to update the column tool_order in a pivot table tool_user for certain records (the ones where order has changed). The Laravel dump server and query result tell me that everything is fine and some records were updated but there are no changes in the table. There are no erros on console, dump server or log. My code:

    public function setOrders(Request $request)
    {
    	$resources = $request['resources'];
        DB::beginTransaction();
        try{
    	  foreach($resources as $resource){
    	  	$query = DB::table('tool_user')
    		    ->where('tool_id', strval($resource['id'])) 
    		    ->where('user_id', Auth::id())
    	            ->update(['tool_order' => $resource['order']]);
    	    dump($resource['id'].' '.Auth::id().' '.$resource['order']);
    	    dump($query);
      	}
      }
      catch(\Exception $e){
        DB::rollBack();
        throw $e;
      };
      return 1; 
    }

Table tool_user has the columns:

id (int(10) )

user_id (int(10) )

tool_id (varchar(64) utf8mb4_unicode_ci)

tool_order (smallint(5))

However, what works fine in another code section is

        $res = DB::table('tool_user')
         ->where('id', $row->id)
         ->update([
           'tool_order' => $newOrder
         ]);
      } 

I checked the docs at https://laravel.com/docs/7.x/queries#updates and can't see any issues with my query. Any ideas?

0 likes
7 replies
SilenceBringer's avatar

Hi @loomix try to dump number of rows which should be affected by every query, possible something wrong with your condition, so really 0 rows matches the query

try{
    foreach($resources as $resource){
        dump($resource['id'].' '.Auth::id().' '.$resource['order']);
        dump(DB::table('tool_user')
            ->where('tool_id', strval($resource['id'])) 
            ->where('user_id', Auth::id())
            ->count());
        $query = DB::table('tool_user')
            ->where('tool_id', strval($resource['id'])) 
            ->where('user_id', Auth::id())
            ->update(['tool_order' => $resource['order']]);
catch(\Exception $e){
    // ...
}
Loomix's avatar

count is a good idea but it's 1 for each resource in the loop. I already checked that in the database and technically the resources is always a list where any user_id and tool_id matches exactly 1 record in the table. The question of the query is only if tool_order gets an update or not.

SilenceBringer's avatar
try{
    foreach($resources as $resource){
        dump('before: ' . DB::table('tool_user')
            ->where('tool_id', strval($resource['id'])) 
            ->where('user_id', Auth::id())
            ->first()
            ->tool_order);
        dump('New value: ' . $resource['order']);
        $query = DB::table('tool_user')
            ->where('tool_id', strval($resource['id'])) 
            ->where('user_id', Auth::id())
            ->update(['tool_order' => $resource['order']]);
        dump('after: ' . DB::table('tool_user')
            ->where('tool_id', strval($resource['id'])) 
            ->where('user_id', Auth::id())
            ->first()
            ->tool_order);
catch(\Exception $e){
    // ...
}
Loomix's avatar

Yeah, the dump server says what is not reflecting in the database: 3 records differ in before and after and obviously the afteris coming from the db but in phpmyAdmin, there are no changes in the table and when I fetch all the resources again, the old order is still there. I don't get it!

First I thought I am somehow on the wrong server or wrong database but this is not the case. Is it possible that there is some kind of buffering issue? Here's an export of the table with 15 records, maybe there's something not right with it:

CREATE TABLE IF NOT EXISTS `tool_user` (
  `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id` int(10) UNSIGNED NOT NULL,
  `tool_id` varchar(64) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NOT NULL,
  `tool_order` smallint(5) UNSIGNED NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  KEY `fk_tool_user_user_id_idx` (`user_id`),
  KEY `fk_tool_user_tool_id_idx` (`tool_id`)
) ENGINE=InnoDB AUTO_INCREMENT=159 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

INSERT INTO `tool_user` (`id`, `user_id`, `tool_id`, `tool_order`) VALUES
(24, 19, '1122', 4),
(25, 19, '1121', 3),
(26, 19, '1123', 5),
(27, 19, '1124', 6),
(28, 19, '1125', 7),
(29, 19, '1126', 8),
(31, 19, '1133', 10),
(36, 19, '1150', 11),
(37, 19, '1153', 12),
(39, 19, '1156', 14),
(42, 19, '1114', 2),
(45, 19, '1110', 1),
(50, 19, '1132', 9),
(51, 20, '1126', 0),
(53, 20, '1123', 0);

Loomix's avatar

I've just found out that it works when I remove the try/catch . But why? Is there anything wrong with how I do it?

Loomix's avatar

omg you're right. i just copied the try/catch and forgot the most important inbetween! xD

Please or to participate in this conversation.