2017-05-05 43 views
0

我有我的觀點的腳本塊一個倒數計時器。 Edit.blade.php:如何調用模型的功能從視圖中的腳本Laravel 4.2

<div class='bottom_timer_block'> 
    <span class="bottime_title">Subscription period</span> 
    <span><b>End date:</b> {{ $paid_till }}</span> 
    <span> 
     <b>Countdown timer: </b> 
     <span id="demo"></span> 
      <script> 
       var countDownDate = new Date('{{ $paid_till }}').getTime();       
       var x = setInterval(function() { 
       // Get todays date and time 
       var now = new Date().getTime(); 

       // Find the distance between now an the count down date 
       var distance = countDownDate - now; 

       // Time calculations for days, hours, minutes and seconds 
       var days = Math.floor(distance/(1000 * 60 * 60 * 24)); 
       var hours = Math.floor((distance % (1000 * 60 * 60 * 24))/(1000 * 60 * 60)); 
       var minutes = Math.floor((distance % (1000 * 60 * 60))/(1000 * 60)); 
       var seconds = Math.floor((distance % (1000 * 60))/1000); 

       // Display the result in the element with id="demo" 
       document.getElementById("demo").innerHTML = days + "d " + hours + "h " 
            + minutes + "m " + seconds + "s "; 

       // If the count down is finished, write some text 
       if (distance < 0) {           
        $.post("/ajax/update-payment", {id:$user->id}).done(function(data) {                       
        }); 

        clearInterval(x);           
        document.getElementById("demo").innerHTML = "EXPIRED"; 
       } 
       }, 1000); 
      </script> 

當計數器達到它的極限。它應該調用位於我的「用戶」模式updatePayment功能。我的AjaxController:

function updatePayment($id = 0){   
    $user = User::where('id', $id); 
    $user->is_paid = 0; 
    $user->paid_date = null; 
    $user->paid_till = null; 
    $user->save(); 
} 

但是,而不是我得到「未捕獲的SyntaxError:意外的令牌>」錯誤。有沒有其他方法可以解決這個問題?

回答

1

那是因爲你是直接通過一個PHP變量的javascript:

{id:$user->id} 

然而,另一個問題是,花括號在刀片模板引擎特殊的意義,所以你應該如下傳遞:

{id: '{{ $user->id }}' } 
相關問題