Home > Enterprise >  Why when I update a specific record a new record is added?
Why when I update a specific record a new record is added?

Time:03-30

When I update a specific record, the record is not updated, but it adds a new record and the record does not update.
I use updateOrCreate() because I used one modal to add and update and I passed the data to the Controller using ajax. Just so you know I have a foreign key and it's airlineId if that might help.

ajax:

 $('body').on('click', '#btn-save', function (event) {
          var id = $("#id").val();
          var flightDesignator = $("#flightDesignator").val();
          var departureFrom = $("#departureFrom").val();
          var arriveTo = $("#arriveTo").val();
          var departureTime = $("#departureTime").val();
          var ArrivalTime = $("#ArrivalTime").val();
          var airlineId = $("#airlineId").val();
          $("#btn-save").html('Please Wait...');
          $("#btn-save"). attr("disabled", true);
         
       
       
        $.ajax({
            type:"POST",
            url: "{{ url('admin/add-update-flights') }}",
            data: {
              id:id,
              flightDesignator:flightDesignator,
              departureFrom:departureFrom,
              arriveTo:arriveTo,
              departureTime:departureTime,
              ArrivalTime:ArrivalTime,
              airlineId:airlineId,
            },
            dataType: 'json',
            success: function(res){
             window.location.reload();
            $("#btn-save").html('Submit');
            $("#btn-save"). attr("disabled", false);
            Swal.fire(
            'Good job!',
            'You clicked the button!',
            'success'
            )
           }
        
        });
      
    });

controller:

public function store(Request $request)
    {
        $Flight   =   Flight::updateOrCreate(
            [
                'FlightId' => $request->id
            ],
          
           
            [
                'flightDesignator' => $request->flightDesignator, 
                'departureFrom' => $request->departureFrom,
                'arriveTo' => $request->arriveTo,
                'departureTime' => $request->departureTime, 
                'ArrivalTime' => $request->ArrivalTime,
                'airlineId' => $request->airlineId,
            ]);
     
                 return response()->json(['success' => true]);
    }


 public function edit(Request $request)
    {   
        $where = array('FlightId' => $request->id);
        $flight  = Flight::where($where)->first();
 
        return response()->json($flight);
    }

CodePudding user response:

easy fix

if($request->id)
{
 $Flight   =   Flight::find($request->id);
}
else{
  $Flight = new Flight();
}

$Flight->flightDesignator = $request->flightDesignator;
/* remaing data*/
$Flight->save();

return response()->json(['success' => true]);

CodePudding user response:

$update_value = YourModel::updateOrCreate(
    ['your_checking_column' => 'your_value',],
    ['your_updated_coumn' => 'your_updated_value']
);

Here first check that the value of $request->id. if the value is empty updateOrCreate() will create a record instead of updating the existing record.

  • Related