在控制器(Laravel)的javascrip通知函数中显示数据



我在javascript中使用通知功能,我想在通知中显示来自控制器的数据

这是我的JavaScript,错误返回

function notifyMe() {
function AutoRefresh( t ) {
setTimeout("location.reload(true);", t);
}
// Let's check if the browser supports notifications
if (!("Notification" in window)) {
alert("This browser does not support desktop notification");
}
// Let's check whether notification permissions have already been granted
else if (Notification.permission === "granted") {
var notification = new Notification({{ $postnotif->title }});
}
else if (Notification.permission !== "denied") {
Notification.requestPermission().then(function (permission) {
if (permission === "granted") {
var notification = new Notification({{ $postnotif->title }});
}
});
}
}
</script>

它返回此错误错误返回

这是我在控制器中的逻辑

$postnotif = DB::table('post_contents')->where('created_at', '=', '2019-03-11 20:07:49')->first();

这是来自$postnotif的 JSON

{#1287
+"id": 1
+"post_id": 1
+"lang": "id"
+"slug": "beranda"
+"title": "Beranda"
+"body": "<p>Website Resmi FIFGROUP, member of ASTRA Perusahaan Pembiayaan Terpercaya di Indonesia.</p>"
+"excerpt": null
+"image": null
+"tag": "astra, pembiayaan, terpercaya, indonesia, cicilan, kredit, pembiayaan motor bekas, pembiayaan motor baru, pembiayaan multiguna, pembiayaan umrah, dana kuliah, m "
+"metas": "a:1:{s:4:"file";N;}"
+"created_at": "2019-03-11 20:07:49"
+"updated_at": "2019-08-30 00:40:47"
}

如何正确地从控制器声明到JavaScript?

您必须通过以下方式访问 Laravel变量:

var notification = new Notification({!! $postnotif->title !!});

相关内容

最新更新