app/Http/Controllers/HomePageController.php
private function updateUserBlocks(int $user_id, array $main_blocks, array $side_blocks): void
    {
        $existing_block_ids = DB::table('block')
            ->where('user_id', '=', $user_id)
            ->pluck('block_id');

        // Deleted blocks
        foreach ($existing_block_ids as $existing_block_id) {
            if (!in_array($existing_block_id, $main_blocks, false) && !in_array($existing_block_id, $side_blocks, false)) {
                DB::table('block_setting')
                    ->where('block_id', '=', $existing_block_id)
                    ->delete();

                DB::table('block')
                    ->where('block_id', '=', $existing_block_id)
                    ->delete();
            }
        }

        $updates = [
            'main' => $main_blocks,
            'side' => $side_blocks,
        ];

        foreach ($updates as $location => $updated_blocks) {
            foreach ($updated_blocks as $block_order => $block_id) {
                if (is_numeric($block_id)) {
                    // Updated block
                    DB::table('block')
                        ->where('block_id', '=', $block_id)
                        ->update([
                            'block_order' => $block_order,
                            'location'    => $location,
                        ]);
                } else {
                    // New block
                    DB::table('block')->insert([
                        'user_id'     => $user_id,
                        'location'    => $location,
                        'block_order' => $block_order,
                        'module_name' => $block_id,
                    ]);
                }
            }
        }
    }
private function updateUserBlocks(int $user_id, array $main_blocks, array $side_blocks): void
    {
        $existing_block_ids = DB::table('block')
            ->where('user_id', '=', $user_id)
            ->pluck('block_id');

        // Deleted blocks
        foreach ($existing_block_ids as $existing_block_id) {
            if (!in_array($existing_block_id, $main_blocks, false) && !in_array($existing_block_id, $side_blocks, false)) {
                DB::table('block_setting')
                    ->where('block_id', '=', $existing_block_id)
                    ->delete();

                DB::table('block')
                    ->where('block_id', '=', $existing_block_id)
                    ->delete();
            }
        }

        $updates = [
            'main' => $main_blocks,
            'side' => $side_blocks,
        ];

        foreach ($updates as $location => $updated_blocks) {
            foreach ($updated_blocks as $block_order => $block_id) {
                if (is_numeric($block_id)) {
                    // Updated block
                    DB::table('block')
                        ->where('block_id', '=', $block_id)
                        ->update([
                            'block_order' => $block_order,
                            'location'    => $location,
                        ]);
                } else {
                    // New block
                    DB::table('block')->insert([
                        'user_id'     => $user_id,
                        'location'    => $location,
                        'block_order' => $block_order,
                        'module_name' => $block_id,
                    ]);
                }
            }
        }
    }
private function updateUserBlocks ( int $ user_id , array $ main_blocks , array $ side_blocks ) : void { $ existing_block_ids = DB :: table ( 'block' ) -> where ( 'user_id' , '=' , $ user_id ) -> pluck ( 'block_id' ) ; // Deleted blocks foreach ( $ existing_block_ids as $ existing_block_id ) { if ( ! in_array ( $ existing_block_id , $ main_blocks , false ) && ! in_array ( $ existing_block_id , $ side_blocks , false ) ) { DB :: table ( 'block_setting' ) -> where ( 'block_id' , '=' , $ existing_block_id ) -> delete ( ) ; DB :: table ( 'block' ) -> where ( 'block_id' , '=' , $ existing_block_id ) -> delete ( ) ; } } $ updates = [ 'main' => $ main_blocks , 'side' => $ side_blocks , ] ; foreach ( $ updates as $ location => $ updated_blocks ) { foreach ( $ updated_blocks as $ block_order => $ block_id ) { if ( is_numeric ( $ block_id ) ) { // Updated block DB :: table ( 'block' ) -> where ( 'block_id' , '=' , $ block_id ) -> update ( [ 'block_order' => $ block_order , 'location' => $ location , ] ) ; } else { // New block DB :: table ( 'block' ) -> insert ( [ 'user_id' => $ user_id , 'location' => $ location , 'block_order' => $ block_order , 'module_name' => $ block_id , ] ) ; } } } }
Save the updated blocks for a user.

@param int   $user_id
@param array $main_blocks
@param array $side_blocks

@return void
Save the updated blocks for a user .