Laravel Delete record functionality

Submitted by Darshan Malani - 1 year ago

Laravel deleter record in db

 public function deleteWater($id){

        $error = false;
        $status_code = 200;
        $result =[];
        if($id != "" && $id > 0) {
            try{
                  $water_intake = \App\Models\WaterIntake::find($id);
                  if($water_intake){
                      $guid = $water_intake->guid;
                      $water_intake->delete();
                      $result["guid"] = $guid;
                      $result['message'] = 'User Water Removed Successfully.';
                  }
                  else{
                      $error = true;
                      $result['error_message'] = 'Record does not exists.';
                  }
               }catch(\Exception $ex){
                    $error = true;
                    $status_code = $ex->getCode();
                    $result['error_code'] = $ex->getCode();
                    $result['error_message'] = $ex->getMessage();
                }
        }else{
            $error =true;
            $status_code = 400;
            $result['message']='Request data is empty';
        }
        return response()->json(['error' => $error,'response'=>$result],$status_code)
                         ->setCallback(\Request::input('callback'));
    }  

Ads cover the costs of maintaining Laravel-tricks

comments powered by Disqus

Ads cover the costs of maintaining Laravel-tricks