2017-05-05 8 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(); 
} 

하지만 그 대신 그

내가 얻을 "catch되지 않은 구문 에러 : 예기치 않은 토큰>"오류입니다. 이 문제를 해결할 다른 방법이 있습니까? 당신이 자바 스크립트에 직접 PHP 변수를 전달되기 때문이다

답변

1

:

{id:$user->id} 

그러나 또 다른 문제는 다음과 같이 당신이 그것을 통과해야하므로 중괄호, 블레이드 템플릿 엔진에서 특별한 의미를 가지고 있다는 것입니다 :

{id: '{{ $user->id }}' }