Krishma
1 month ago
195
7
Laravel

How to avoid duplicate values at the time of update query using if and else condition in laravel controller

Posted 1 month ago by Krishma
function update(Request $request,$id)
        {
            $category_code = $request->input('category_code');
            $category_desc = $request->input('category_desc');
            $modified_on = date('Y-m-d');
            $modified_by = 0;

            $mysql = DB::table('asset_category')->where('category_id',$id)->select($category_code);
            if($mysql==$category_code)
            {
            $data= array('category_code'=>$category_code,'category_desc'=>$category_desc,'modified_on'=>$modified_on,'modified_by'=>$modified_by);
            DB::table('asset_category')->where('category_id',$id)->update($data);
            }
            else{
                $cnt= DB::table('asset_category')->where('category_code',$category_code)->count();
                if($cnt > 0){
                    return redirect('AssetCategoryController/update/{id}')->with('category_code', $category_code)->withInput()->withErrors('Data Already Exist');
                }else{
                    $data= array('category_code'=>$category_code,'category_desc'=>$category_desc,'modified_on'=>$modified_on,'modified_by'=>$modified_by);
                    DB::table('asset_category')->where('category_id',$id)->update($data);
                }
            }
            
        }

i have tried this but i m not getting any output

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