Home > Blockchain >  Laravel AJAX - update model when find record or create new record in database
Laravel AJAX - update model when find record or create new record in database

Time:10-09

i have problem with my code. I use Ajax in Laravel. I have a database with 2 tables: jobs and timers. In timers, I store the users' working time. The structure of the database is as follows: job_id, user_id, status, started, stopped,

Ajax works OK when the record is missing from the database - the user with job_id was not found in the Timers table. However, it does not update when this record from user_id is already in the database. My controller function:

public function timerUpdate(Request $request) {
    $timer = Timer::where('job_id',$request->job_id)->where('user_id',auth()->user()->id)->first();
    if($timer){
    if($request->status == '0'){
        $timer->update([
        'stopped' => new Carbon,
        ]);
    }else if($request->status == '1'){
        $timer->update([
        'stopped' => NULL,
        ]);
    }
    return json_encode(array('statusCode'=>200));
    }else{
                Timer::create([
                                'user_id' => auth()->user()->id,
                                'job_id' => $request->job_id,
                                'started' => new Carbon,
                            ]);
        return json_encode(array('statusCode'=>200));
        
    }
}

my Ajax is run in a view by changing the checkbox. My code:

<script type='text/javascript'>
var CSRF_TOKEN = $('meta[name="csrf-token"]').attr('content');

$(document).ready(function() {
// Update record
$('.toggle-class').change(function() {
        var status = $(this).prop('checked') == true ? 1 : 0;
        var user_id = $(this).data('userId');
        var task_id = $(this).data('id');

    $.ajax({
      url: '{{ route('timerUpdate') }}',
      type: 'post',
      data: {_token: CSRF_TOKEN,task_id: task_id,running: running},
      success: function(data){
                $('#message').fadeIn();
                $('#message').text('Timer Updated Successfully');
                setTimeout(() => {
                    $('#message').fadeOut();
                }, 3000);
      }
    });
  
});

});

</script>

In my opinion not working problem is here:

$timer = Timer::where('task_id',$request->task_id)->where('user_id',auth()->user()->id)->first(); if($timer){}

If user_id and job_id was not found in the database, code working - creates a new record in the database. But not working when in database record found - function update not working. Please help. I don't know what I'm doing wrong with my code. Is the problem in the question or maybe AJAX can be improved?

CodePudding user response:

Make sure you add the following to your model


class Timer extends Model
{
    protected $fillable = [
        'stopped',
        'started'
    ];
}

I hope it helps you. Have a good one :)

CodePudding user response:

As I see, you have not passed job_id and status as your data from ajax to your controller and then in your controller you have used $request->job_id and $request->status. Maybe your problem is related to this.

$.ajax({
   data: {
       '_token' : CSRF_TOKEN,
       'task_id' : task_id,
       'status' : status,
       'job_id' : ...

   },
})

  • Related