app/Http/Controllers/Admin/MainCategoriesController.php
// UPDATE MAIN CATEGORY EDIT FORM DATA
UPDATE MAIN CATEGORY EDIT FORM DATA
UPDATE MAIN CATEGORY EDIT FORM DATA
public function update(MainCateRequest $request, $cate_id)
    {
        try {
            $cate = MainCate::find($cate_id);
            if (!$cate) {
                return redirect()->route('admin.main.cate.edit')->with([
                    'error' => 'No such main category'
                ]);
            }
            DB::beginTransaction();
            if ($request->has('cate_imag')) {
                if ($cate) {
                    deleteFile($cate->photo);
                }
                $imgPath = uploadFile(
                    'main_cates',           
                    $request->cate_imag     
                );
                $cate->update([
                    'photo' => $imgPath
                ]);
            }
            $defLangCate =  array_values(
                $request->cate_bags
            )[0];
            $defCateTrans = array_merge(
                $defLangCate,           
                [
                    'cate_trans' => 0   
                ]
            )['cate_trans'];            
            if (!array_key_exists('cate_stat', $defLangCate)) {
                $defCateStat = array_merge(     
                    $defLangCate,               
                    [
                        'cate_stat' => 0        
                    ]
                )['cate_stat'];                 
            } else {
                $defCateStat = intval(          
                    $defLangCate['cate_stat']   
                );
            }
            $cate->update([
                    'name'          => ucfirst($defLangCate['cate_name']),      
                    'trans_lang'    => strtoupper($defLangCate['cate_abbr']),   
                    'trans_of'      => $defCateTrans,                           
                    'status'        => $defCateStat                             
                ]);
            $cateInDiffLangs =  array_values(
                $request->cate_bags
            );
            if (isset($cateInDiffLangs) && count($cateInDiffLangs) > 0) {
                if (array_key_exists(0, $cateInDiffLangs)) {
                    unset($cateInDiffLangs[0]);
                }
                if (isset($cateInDiffLangs) && count($cateInDiffLangs) > 0) {
                    foreach ($cateInDiffLangs as $cateInOneLang) {
                        if (!array_key_exists('cate_stat', $cateInOneLang)) {
                            $cateStat = array_merge(    
                                $cateInOneLang,         
                                [
                                    'cate_stat' => 0    
                                ]
                            )['cate_stat'];             
                        } else {
                            $cateStat = intval(                 
                                $cateInOneLang['cate_stat']     
                            );
                        }
                        $cateTranslations[] = [
                            'id'            => intval($cateInOneLang['cate_id']),               
                            'name'          => ucfirst($cateInOneLang['cate_name']),            
                            'trans_lang'    => strtoupper($cateInOneLang['cate_abbr']),         
                            'trans_of'      => intval($cateInOneLang['cate_trans']),            
                            'status'        => $cateStat                                        
                        ];
                    }
                }
            }
            if (isset($cateTranslations) && count($cateTranslations) > 0) {
                foreach ($cateTranslations as $cateTranslation) {
                    $cateTranslationID = MainCate::find($cateTranslation['id']);
                    if ($request->has('cate_imag')) {
                        if ($cateTranslationID) {
                            deleteFile($cateTranslationID->photo);
                        }
                        $imgPath = uploadFile('main_cates', $request->cate_imag);
                        $cateTranslationID->update([
                            'photo' => $imgPath
                        ]);
                    }
                    $cateTranslationID->update([
                        'name'          => $cateTranslation['name'],            
                        'trans_lang'    => $cateTranslation['trans_lang'],      
                        'trans_of'      => $cateTranslation['trans_of'],        
                        'status'        => $cateTranslation['status']           
                    ]);
                }
            }
            DB::commit();
            return redirect()->route('admin.main.cates')->with([
                'success' => 'Updated Successfully'
            ]);
        } catch (\Throwable $th) {
            DB::rollBack();
            return redirect()->route('admin.main.cate.edit')->with([
                'error' => 'something went terribly wrong'
            ]);
        }
    }
public function update ( MainCateRequest $ request , $ cate_id ) { try { $ cate = MainCate :: find ( $ cate_id ) ; if ( ! $ cate ) { return redirect ( ) -> route ( 'admin.main.cate.edit' ) -> with ( [ 'error' => 'No such main category' ] ) ; } DB :: beginTransaction ( ) ; if ( $ request -> has ( 'cate_imag' ) ) { if ( $ cate ) { deleteFile ( $ cate -> photo ) ; } $ imgPath = uploadFile ( 'main_cates' , $ request -> cate_imag ) ; $ cate -> update ( [ 'photo' => $ imgPath ] ) ; } $ defLangCate = array_values ( $ request -> cate_bags ) [ 0 ] ; $ defCateTrans = array_merge ( $ defLangCate , [ 'cate_trans' => 0 ] ) [ 'cate_trans' ] ; if ( ! array_key_exists ( 'cate_stat' , $ defLangCate ) ) { $ defCateStat = array_merge ( $ defLangCate , [ 'cate_stat' => 0 ] ) [ 'cate_stat' ] ; } else { $ defCateStat = intval ( $ defLangCate [ 'cate_stat' ] ) ; } $ cate -> update ( [ 'name' => ucfirst ( $ defLangCate [ 'cate_name' ] ) , 'trans_lang' => strtoupper ( $ defLangCate [ 'cate_abbr' ] ) , 'trans_of' => $ defCateTrans , 'status' => $ defCateStat ] ) ; $ cateInDiffLangs = array_values ( $ request -> cate_bags ) ; if ( isset ( $ cateInDiffLangs ) && count ( $ cateInDiffLangs ) > 0 ) { if ( array_key_exists ( 0 , $ cateInDiffLangs ) ) { unset ( $ cateInDiffLangs [ 0 ] ) ; } if ( isset ( $ cateInDiffLangs ) && count ( $ cateInDiffLangs ) > 0 ) { foreach ( $ cateInDiffLangs as $ cateInOneLang ) { if ( ! array_key_exists ( 'cate_stat' , $ cateInOneLang ) ) { $ cateStat = array_merge ( $ cateInOneLang , [ 'cate_stat' => 0 ] ) [ 'cate_stat' ] ; } else { $ cateStat = intval ( $ cateInOneLang [ 'cate_stat' ] ) ; } $ cateTranslations [ ] = [ 'id' => intval ( $ cateInOneLang [ 'cate_id' ] ) , 'name' => ucfirst ( $ cateInOneLang [ 'cate_name' ] ) , 'trans_lang' => strtoupper ( $ cateInOneLang [ 'cate_abbr' ] ) , 'trans_of' => intval ( $ cateInOneLang [ 'cate_trans' ] ) , 'status' => $ cateStat ] ; } } } if ( isset ( $ cateTranslations ) && count ( $ cateTranslations ) > 0 ) { foreach ( $ cateTranslations as $ cateTranslation ) { $ cateTranslationID = MainCate :: find ( $ cateTranslation [ 'id' ] ) ; if ( $ request -> has ( 'cate_imag' ) ) { if ( $ cateTranslationID ) { deleteFile ( $ cateTranslationID -> photo ) ; } $ imgPath = uploadFile ( 'main_cates' , $ request -> cate_imag ) ; $ cateTranslationID -> update ( [ 'photo' => $ imgPath ] ) ; } $ cateTranslationID -> update ( [ 'name' => $ cateTranslation [ 'name' ] , 'trans_lang' => $ cateTranslation [ 'trans_lang' ] , 'trans_of' => $ cateTranslation [ 'trans_of' ] , 'status' => $ cateTranslation [ 'status' ] ] ) ; } } DB :: commit ( ) ; return redirect ( ) -> route ( 'admin.main.cates' ) -> with ( [ 'success' => 'Updated Successfully' ] ) ; } catch ( \ Throwable $ th ) { DB :: rollBack ( ) ; return redirect ( ) -> route ( 'admin.main.cate.edit' ) -> with ( [ 'error' => 'something went terribly wrong' ] ) ; } }
UPDATE MAIN CATEGORY EDIT FORM DATA
UPDATE MAIN CATEGORY EDIT FORM DATA
// FIND MAIN CATEGORY OF THE DEFAULT LANGUAGE // CHECK MAIN CATEGORY NOT EXIST IN DATABASE // REDIRECT TO MAIN CATEGORY TABLE WITH ERROR MESSAGE // START DATABASE STATEMENTS TRANSACTIONS // NEW IMAGE UPLOADED CHECK // CHECK MAIN CATEGORY EXISTANCE IN DATABASE // DELETE THE OLD MAIN CATEGORY IMAGE FROM SERVER FOLDER // NEW UPLOADED TO SERVER MAIN CATEGORY IMAGE PATH TO BE STORED IN DATABASE // SERVER FOLDER WHERE TO UPLOAD // FILE NAME WHICH TO STORE // DATABASE UPDATE NEW IMAGE STATEMENT // MAIN CATEGORY ARRAY OF INPUT DATA FOR THE DEFAULT LANGUAGE // MERGE THE TRANSLATION LANGUAGE VALUE OF ZERO AS IT IS THE DEFAULT LANGUAGE // MAIN CATEGORY ARRAY OF INPUT DATA FOR THE DEFAULT LANGUAGE // KEY AND VALUE TO BE ADDED TO THE DEFAULT MAIN CATEGORY ARRAY DATE // RETRIEVE THE MERGED VALUE OF TRANSLATION LANGUAGE KEY // CHECK DEFAULT MAIN CATEGORY IS EDITED TO BE DEACTIVATED // APPEND THE VALUE OF ZERO FOR DEACTIVATION // MAIN CATEGORY ARRAY OF INPUT DATA FOR THE DEFAULT LANGUAGE // KEY AND VALUE OF DEACTIVATION TO BE MERGED TO THE ARRAY // RETRIEVEMENT OF THE THE MERGED DEFAULT MAIN CATEGORY STATUS // CONVERT THE VALUE OF ACTIVATION FROM STRING AS INSERTED TO INTEGER // DEFAULT MAIN CATEGORY ACTIVATION INITIALLY INSERTED VALUE // DEFAULT LANGUAGE MAIN CATEGORY UPDATE STATEMENT // DEFAULT MAIN CATEGORY NAME STARTED WITH UPPER CASE LETTER // DEFAULT MAIN CATEGORY ABBREVIATION OF TRANSLATION LANGUAGE IN UPPDER CASE // DEFAULT MAIN CATEGORY TRANSLATION LANGUAGE WHICH IS BASICALLY ZERO // DEFAULT MAIN CATEGORY STATUS VALUE ZERO OR ONE ################################################### // ARRAY OF ALL DEFAULT AND TRANSLATED CATEGORIES // CHECK ALL DEFAULT AND TRANSLATED CATEGORIES // CHECK DEFAULT CATEGORY EXISTS IN GENERAL ARRAY // REMOVE THE DEFAULT CATEGORY // CHECK ALL TRANSLATED CATEGORIES // TRANSLATED CATEGORIES LOOP // CHECK IF THE TRANSLATED CATEGORY IS DEACTIVATED // MERGE DEACTIVATION VALUE TO THE TRANSLATED CATEGORY ARRAY // TRANSLATED CATEGORY ARRAY WHERE TO MERGE // DEACTIVATION KAY AND VALUE TO BE MERGED // RETRIEVEMENT OF THE DEACTIVATION VALUE ADDED // CONVERT THE DEFAULT INSERTED ACTIVATION VALUE FROM A STRING TO INTEGER // THE DEFAULT INSERTED ACTIVATION STRING VALUE // ARRAY OF TRANSLATED CATEGORY DATA TO BE ADDED // TRANSLATED CATEGORY ID AS INTEGER VALUE // TRANSLATED CATEGORY NAME WITH FIRST LETTER IN UPPER CASE // TRANSLATED CATEGORY LANGUAGE OF TRANSLATION IN UPPER CASE // TRANSLATED CATEGORY LANGUAGE OF TRANSLATION AS INTEGER // TRANSLATED CATEGORY STATUS WHETHER ACTIVE OR NOT // CHECK WHETHER ARRAY OF TRANSLATED CATEGORY DATA EXISTS AND  EMPTY  // TRANSLATED CATEGORY DATA LOOP // DATABASE TRANSLATED CATEGORY OF AN ID // NEW IMAGE UPLOADED CHECK // DATABASE TRANSLATED CATEGORY EXISTANCE CHECK // DELETE THE OLD TRANSLATED CATEGORY IMAGE FROM SERVER // UPLOAD THE NEW CATEGORY IMAGE TO ITS FOLDER ON SERVER // DATABASE UPDATE TRANSLATED CATEGORY IMAGE STATEMENT // DATABASE UPDATE THE TRANSLATED CATEGORY DATA STATEMENT // TRANSLATED CATEGORY NAME // TRANSLATED CATEGORY TRANSLATION LANGUAGE // TRANSLATED CATEGORY DEFAULT LANGUAGE OF TRANSLATION // TRANSLATED CATEGORU STATUS WHETHE ACTIVE OR NOT // COMMIT DATABASE STATEMENTS TRANSACTIONS // REDIRECT TO MAIN CATEGORIES TABLE WITH SUCCESS MESSAGE // CHECK ERRORS AND IF EXIST DON'T IMPLEMENT ANY OF THE PREVIOUS TRANSACTIONS // REDIRECT TO MAIN CATEGORY EDIT FORM WITH ERROR MESSAGE