title: How to save video playing currentTime in Laravel using ajax
body: <p>This is my route which shows the lesson.</p>
  <pre><code>Route::get('/lession_video/{lession_id}','FrontendCommonController@lession_video')-&gt;name('lession_video');
                  
  </code></pre>
  <p>Here is my another route which I am using for saving video currentTime in the database using ajax</p>
  <pre><code>Route::post('/saveTime/{video}', 'InstructorCourseController@saveTime')-&gt;name('video.saveTime');
  </code></pre>
  <p>This is one method which I am using for updating the video currentTime in database</p>
  <pre class="lang-js prettyprint-override"><code>public function saveTime(Request $request, $lession_id){
  
      $user = Auth::user();
      if($user === null){
          return response()-&gt;json(['message' =&gt; 'User not authenticated', 404]);
      }
  
      $video = \App\Lession::where('id',$lession_id)-&gt;first();
  
      $currentTime = $request-&gt;time;
      $duration = $request-&gt;duration;
        // return $duration;
      //save them somewhere
  
      $watch_history = $user-&gt;watch_history;
      $watch_history_array = array();
      if ($watch_history == '') {
        array_push($watch_history_array, array('lesson_id' =&gt; $lession_id, 'progress' =&gt; $currentTime));
      } else {
        $founder = false;
        $watch_history_array = json_decode($watch_history, true);
        for ($i = 0; $i &lt; count($watch_history_array); $i++) {
          $watch_history_for_each_lesson = $watch_history_array[$i];
          if ($watch_history_for_each_lesson['lesson_id'] == $video-&gt;id) {
            $watch_history_for_each_lesson['progress'] = $currentTime;
            // $watch_history_array[$i]['progress'] = $progress;
            $founder = true;
          }
        }
        if (!$founder) {
        array_push($watch_history_array, array('lesson_id' =&gt; $video-&gt;id, 'progress' =&gt; $currentTime));
        }
      }
      // return $watch_history_array;
      $data['watch_history'] = json_encode($watch_history_array);
      
      $check = User::where('id',$user-&gt;id)-&gt;update(['watch_history' =&gt; $watch_history_array]);
  
      return response()-&gt;json(['message' =&gt; 'Time saved', 200]);//send http response as json back to the ajax call
  
  }
  </code></pre>
  <p>Next, this is my video player on course_video.blade.php file</p>
  <pre class="lang-html prettyprint-override"><code>&lt;video id=&quot;player&quot; poster=&quot;/public/images/{{$lession-&gt;thumbnail}}&quot; class=&quot;plyr&quot;&gt;
    &lt;input type=&quot;hidden&quot; name=&quot;_token&quot; value=&quot;{{csrf_token()}}&quot; /&gt;
    &lt;source src=&quot;/public/images/{{$lession-&gt;resource}}&quot; /&gt;
  &lt;/video&gt;
  </code></pre>
  <p>Then next on the same Blade page I have my ajax code as follows</p>
  <pre class="lang-js prettyprint-override"><code>&lt;script&gt;
    var vid = document.getElementById(&quot;player&quot;);
  
    $(function() {
      var timeout;
  
      $(&quot;#player&quot;).on(&quot;playing pause&quot;, function(e) {
        // save reference
        var v = this;
  
        // clear previous timeout, if any
        clearTimeout(timeout);
  
        // call immediately if paused or when started
        performaction(v.currentTime, v.duration);
  
        // set up interval to fire very 5 seconds
        if (e.type === &quot;playing&quot;) {
          timeout = setInterval(function() {
            performaction(v.currentTime, v.duration);
          }, 1000);
        }
      });
  
      function performaction(currentTime, duration, lession_id) {
        //pass video id to this function where you call it.
        var data = { time: currentTime, duration: duration }; //data to send to server
        var dataType = &quot;json&quot;; //expected datatype from server
        var headers = { &quot;X-CSRF-TOKEN&quot;: $('input[name=&quot;_token&quot;]').val() };
        $.ajax({
          type: &quot;POST&quot;,
          url: '{{route(&quot;video.saveTime&quot;,$lession_id)}}', //url of the server which stores time data
          data: data,
          headers: headers,
          success: function(data, status) {
            alert(status);
            var data = JSON.parse(data);
            alert(data[&quot;message&quot;]);
          },
          dataType: dataType
        });
      }
    });
  &lt;/script&gt;
  </code></pre>
  <p>I want to store this currentTime in the database but this code is not working and even I didn't get it how the route for savetime and its method savetime() gets executed.</p>