app/Http/Controllers/CategoryController.php
//    ---- remove parent and child categories,products(info_product) withe categories,products and info_products tables
remove parent and child categories,products(info_product) withe categories,products and info_products tables
remove parent and child categories products ( info_product ) withe categories products and info_products tables
public function product_Remove(Request $request){
        if(is_array($request->product_array))
        {
            $translation_table = TranslationLang::all();
            if($request->product == 'save'){
                foreach ($request->product_array as $req) {
                    dd($request->product_array);
                    $id = Product::where('name',$req)->get();
                    $destinationPath = public_path('img');
                    $name = $id[0]["img"];
                    unlink("$destinationPath/$name");
                    if ($request->hasFile('img')) {
                        $image = $req->file('img');
                        $name = time() . '.' . $image->getClientOriginalExtension();
                        $destinationPath = public_path('/img');
                        $name = Crypt::encrypt($name);
                        $image->move($destinationPath, $name);
                    } else {
                        $name = null;
                    }
                    foreach ($translation_table as $language){
                        $lang = $language->folder;
                        $file = $lang.'.json';
                        $destinationPath=storage_path('..\resources\lang\\');
                        $path_ru = storage_path('..\resources\lang\\'.$file);
                        $lang = json_decode(file_get_contents($path_ru), true);
                        $lang[$req->product_name] = $req->product_name;
                        $lang[$req->product_description] = $req->product_description;
                        File::put($destinationPath.$file, json_encode($lang));
                    }
                    Product::ubdate([
                        'parent_id' => $req->id,
                        'img' => $name,
                        'name' => $req->product_name,
                        'descriptions' => $req->product_description,
                        'price' => $req->price,
                    ]);
                }
            } else{
                foreach ($request->product_array as $req){
                    $id = Product::where('name',$req)->get();
                    $destinationPath = public_path('img');
                    $name = $id[0]["img"];
                    unlink("$destinationPath/$name");
                    DB::delete('delete from categories where name = ?',[$req]);
                    DB::delete('delete from products where name = ?',[$req]);
                    DB::delete('delete from info_product where parent_id = ?',[ $id[0]["id"]]);
                }
            }
        }else{
            if($request->product == 'save'){
                $id = Product::where('name',$request->product_name)->get();
                if ($request->hasFile('img')) {
                    $destinationPath = public_path('img');
                    $name = $id[0]["img"];
                    if($name){
                        unlink("$destinationPath/$name");
                    }
                    $image = $request->file('img');
                    $name = time() . '.' . $image->getClientOriginalExtension();
                    $destinationPath = public_path('/img');
                    $name = Crypt::encrypt($name);
                    $image->move($destinationPath, $name);
                } else {
                    $name = $id[0]->img;
                }
                $translation_table = TranslationLang::all();
                foreach ($translation_table as $language){
                    $lang = $language->folder;
                    $file = $lang.'.json';
                    $destinationPath=storage_path('..\resources\lang\\');
                    $path_ru = storage_path('..\resources\lang\\'.$file);
                    $lang = json_decode(file_get_contents($path_ru), true);
                    $lang[$request->edit_product_name] = $request->edit_product_name;
                    $lang[$request->product_description] = $request->product_description;
                    File::put($destinationPath.$file, json_encode($lang));
                }
                Category::where('name',$request->product_name)->update([
                    'name' => $request->edit_product_name,
                    'category_id' => $request->id,
                ]);
                Product::where('name',$request->product_name)->update([
                    'parent_id' => $request->id,
                    'name' => $request->edit_product_name,
                    'descriptions' => $request->product_description,
                    'img' => $name,
                    'price' => $request->price,
                ]);
            } else{
                $id = Product::where('name', $request->product_name)->get();
                $destinationPath = public_path('img');
                $name = $id[0]["img"];
                unlink("$destinationPath/$name");
                DB::delete('delete from categories where name = ?', [$request->product_name]);
                DB::delete('delete from products where name = ?', [$request->product_name]);
                DB::delete('delete from info_product where parent_id = ?', [$id[0]["id"]]);
            }
        }
        unset($lang);
        return redirect()->back();
    }
public function product_Remove ( Request $ request ) { if ( is_array ( $ request -> product_array ) ) { $ translation_table = TranslationLang :: all ( ) ; if ( $ request -> product == 'save' ) { foreach ( $ request -> product_array as $ req ) { dd ( $ request -> product_array ) ; $ id = Product :: where ( 'name' , $ req ) -> get ( ) ; $ destinationPath = public_path ( 'img' ) ; $ name = $ id [ 0 ] [ " img " ] ; unlink ( " $ destinationPath / $ name " ) ; if ( $ request -> hasFile ( 'img' ) ) { $ image = $ req -> file ( 'img' ) ; $ name = time ( ) . '.' . $ image -> getClientOriginalExtension ( ) ; $ destinationPath = public_path ( '/img' ) ; $ name = Crypt :: encrypt ( $ name ) ; $ image -> move ( $ destinationPath , $ name ) ; } else { $ name = null ; } foreach ( $ translation_table as $ language ) { $ lang = $ language -> folder ; $ file = $ lang . '.json' ; $ destinationPath = storage_path ( '..\resources\lang\\' ) ; $ path_ru = storage_path ( '..\resources\lang\\' . $ file ) ; $ lang = json_decode ( file_get_contents ( $ path_ru ) , true ) ; $ lang [ $ req -> product_name ] = $ req -> product_name ; $ lang [ $ req -> product_description ] = $ req -> product_description ; File :: put ( $ destinationPath . $ file , json_encode ( $ lang ) ) ; } Product :: ubdate ( [ 'parent_id' => $ req -> id , 'img' => $ name , 'name' => $ req -> product_name , 'descriptions' => $ req -> product_description , 'price' => $ req -> price , ] ) ; } } else { foreach ( $ request -> product_array as $ req ) { $ id = Product :: where ( 'name' , $ req ) -> get ( ) ; $ destinationPath = public_path ( 'img' ) ; $ name = $ id [ 0 ] [ " img " ] ; unlink ( " $ destinationPath / $ name " ) ; DB :: delete ( 'delete from categories where name = ?' , [ $ req ] ) ; DB :: delete ( 'delete from products where name = ?' , [ $ req ] ) ; DB :: delete ( 'delete from info_product where parent_id = ?' , [ $ id [ 0 ] [ " id " ] ] ) ; } } } else { if ( $ request -> product == 'save' ) { $ id = Product :: where ( 'name' , $ request -> product_name ) -> get ( ) ; if ( $ request -> hasFile ( 'img' ) ) { $ destinationPath = public_path ( 'img' ) ; $ name = $ id [ 0 ] [ " img " ] ; if ( $ name ) { unlink ( " $ destinationPath / $ name " ) ; } $ image = $ request -> file ( 'img' ) ; $ name = time ( ) . '.' . $ image -> getClientOriginalExtension ( ) ; $ destinationPath = public_path ( '/img' ) ; $ name = Crypt :: encrypt ( $ name ) ; $ image -> move ( $ destinationPath , $ name ) ; } else { $ name = $ id [ 0 ] -> img ; } $ translation_table = TranslationLang :: all ( ) ; foreach ( $ translation_table as $ language ) { $ lang = $ language -> folder ; $ file = $ lang . '.json' ; $ destinationPath = storage_path ( '..\resources\lang\\' ) ; $ path_ru = storage_path ( '..\resources\lang\\' . $ file ) ; $ lang = json_decode ( file_get_contents ( $ path_ru ) , true ) ; $ lang [ $ request -> edit_product_name ] = $ request -> edit_product_name ; $ lang [ $ request -> product_description ] = $ request -> product_description ; File :: put ( $ destinationPath . $ file , json_encode ( $ lang ) ) ; } Category :: where ( 'name' , $ request -> product_name ) -> update ( [ 'name' => $ request -> edit_product_name , 'category_id' => $ request -> id , ] ) ; Product :: where ( 'name' , $ request -> product_name ) -> update ( [ 'parent_id' => $ request -> id , 'name' => $ request -> edit_product_name , 'descriptions' => $ request -> product_description , 'img' => $ name , 'price' => $ request -> price , ] ) ; } else { $ id = Product :: where ( 'name' , $ request -> product_name ) -> get ( ) ; $ destinationPath = public_path ( 'img' ) ; $ name = $ id [ 0 ] [ " img " ] ; unlink ( " $ destinationPath / $ name " ) ; DB :: delete ( 'delete from categories where name = ?' , [ $ request -> product_name ] ) ; DB :: delete ( 'delete from products where name = ?' , [ $ request -> product_name ] ) ; DB :: delete ( 'delete from info_product where parent_id = ?' , [ $ id [ 0 ] [ " id " ] ] ) ; } } unset ( $ lang ) ; return redirect ( ) -> back ( ) ; }
remove parent and child categories,products(info_product) withe categories,products and info_products tables
remove parent and child categories products ( info_product ) withe categories products and info_products tables