'do not show the success message if the data hasnt been updated n a form laravel

i have a form whereby on updating the data and storing it to the database it shows a success message.if one of the inputs isn't filled it shows an error.am getting a bug whereby when i want to re-update the data and i open the form with the existing inputs when i click save the data should just redirect back to the previous page and not show the success message as the data hasnt being updated.how can i achieve this,am looking for a logic here fellow devs..here is my update function code

   public function update(Request $request)
{
    try {

        $validation = Validator::make($request->all(), [
            'systemid' => 'required',
            'category' => 'required',
            'subcategory' => 'required',
            'prdcategory' => 'required',
            'prdbrand' => 'required'
        ]);

        Log::debug('Request: '.json_encode($request->file()));

        if ($validation->fails()) {
            throw new \Exception("validation_error", 19);
        }

        $systemid = $request->systemid;
        $product_details = product::where('systemid', $systemid)->first();
    
        
        $changed = false;

        if ($request->has('product_name')) {
            if ($product_details->name != $request->product_name) {
                $product_details->name = $request->product_name;
                $changed = true;
            }
        }

        if ($request->has('category')) {
            if ($product_details->prdcategory_id != $request->category) {
                $product_details->prdcategory_id = $request->category;
                $changed = true;
            }
        }


        if ($request->has('subcategory')) {
            if ($product_details->prdsubcategory_id != $request->subcategory) {
                $product_details->prdsubcategory_id = $request->subcategory;
                $changed = true;
            }

            if ($product_details->ptype == 'voucher') {
                $voucher = voucher::where('product_id', $product_details->id)->first();
                if($voucher->subcategory_id != $request->subcategory){
                    $voucher->subcategory_id = $request->subcategory;

                    $voucher->save();

                    $changed = true;
                }
            }
        }

        if ($request->has('prdcategory')) {
            if ($product_details->prdprdcategory_id != $request->prdcategory) {
                $product_details->prdprdcategory_id = $request->prdcategory;
                $changed = true;
            }
        }


        if ($request->has('prdbrand')) {
            if ($product_details->brand_id != $request->prdbrand) {
                $product_details->brand_id = $request->prdbrand;
                $changed = true;
            }
        }

        if ($request->has('description')) {
            if ($product_details->description != $request->description) {
                $product_details->description = $request->description;
                $changed = true;
            }
        }
        if ($changed == true || true) {
            $product_details->save();
            $msg = "Product information updated successfully";
            $data = view('layouts.dialog', compact('msg'));

                //i have added this code but it doesnt work
        } else if($changed == false) {
            return back();
            $data = '';
        }
    }
    return $data;
}

my laravel project version is 5.8



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source