Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

119
Vistas
Why when I update a specific record a new record is added?

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);
    }
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

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]);
over 4 years ago · Santiago Trujillo Denunciar

0

$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.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda