Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

210
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!