Loomix
1 week ago
142
7
Laravel

Positive query result but no database update

Posted 1 week ago by Loomix

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?

Please sign in or create an account to participate in this conversation.