Home > Net >  do not show the success message if the data hasnt been updated n a form laravel
do not show the success message if the data hasnt been updated n a form laravel

Time:03-02

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

CodePudding user response:

The following line will always evaluate to True $changed == true || true

And you have a catch statement missing at the end so I had to add it.

And I advise you to simply get the dirty version of $product_details.

You can use $product_details->isDirty() // boolean.

Or even better way is to use $product_details->wasChanged() // boolean

Here is the code after some tweaks:

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;
        // Looping for all inputs:
        $fieldsToCheck = [
            'name' => 'product_name',
            'prdcategory_id' => 'category',
            'prdsubcategory_id' => 'subcategory',
            'prdprdcategory_id' => 'prdcategory',
            'brand_id' => 'prdbrand',
            'description' => 'description',
        ];
        foreach ($fieldsToCheck as $productColumnName => $requestFieldName) {
            $requestInput = $request->{$requestFieldName};
            if ($request->has($requestFieldName)) {
                if ($product_details->$productColumnName != $requestInput) {
                    $product_details->$productColumnName = $requestInput;
                    $changed = true;
                }
            }
            // Exception for Sub Category to check for the voucher.
            if ($requestFieldName == 'subcategory') {
                $this->handleVoucher($requestInput);
            }
        }


        // here I advise you to simply get the dirty version of $product_details
        // you can use $product_details->isDirty() // boolean
        // or even better use $product_details->wasChanged() // boolean
        if ($changed) {
            $product_details->save();
            $msg = 'Product information updated successfully';
            $data = view('layouts.dialog', compact('msg'));

        } else {
            return back();
            // Todo Mo: No need for this line so I commented it out.
            //$data = '';
        }
    } catch (\Exception $e) {
        dd($e->getMessage(), 'Oops, error occurred');
    }

    return $data;
}

private function handleVoucher($product_details, $subcategory)
{
    if ($product_details->ptype == 'voucher') {
        $voucher = voucher::where('product_id', $product_details->id)->first();
        if ($voucher->subcategory_id != $subcategory) {
            $voucher->subcategory_id = $subcategory;
            $voucher->save();
        }
    }
}
  • Related