gpt4 book ai didi

javascript - 如何从 Laravel 4.2 中的 View 脚本调用模型函数

转载 作者:行者123 更新时间:2023-12-03 04:24:24 26 4
gpt4 key购买 nike

我的 View 脚本 block 中有一个倒计时器。编辑.blade.php:

<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 函数。我的 Ajax Controller :

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

但我却收到“Uncaught SyntaxError: Unexpected token >”错误。还有其他方法可以解决这个问题吗?

最佳答案

那是因为您将 php 变量直接传递给 javascript :

{id:$user->id}

但是,另一个问题是花括号在 Blade 模板引擎中具有特殊含义,因此您应该按如下方式传递它:

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

关于javascript - 如何从 Laravel 4.2 中的 View 脚本调用模型函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43804268/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com