php - 如何将参数传递给刀片或 {{ }}?
问题描述
对不起我糟糕的英语。我来自台湾。
源代码
var channel = pusher.subscribe('question-channel');
channel.bind('question-event', function (data) {
var n = new Notification(data.title, {
icon: 'img/icon.png',
body: data.content,
image:'img/1.jpg'
});
n.onclick = function(e) {
e.preventDefault(); // prevent the browser from focusing the Notification's tab
window.open('http://127.0.0.1:8000/');
}
});
我想让它成为
var channel = pusher.subscribe('question-channel');
channel.bind('question-event', function (data) {
if({{\App\Models\UserAreas::where('user_id','=',Auth::id())->where('area_id','=',data.area)}})
var n = new Notification(data.title, {
icon: 'img/icon.png',
body: data.content,
image:'img/1.jpg'
});
n.onclick = function(e) {
e.preventDefault(); // prevent the browser from focusing the Notification's tab
window.open('http://127.0.0.1:8000/');
}
});
但是,我无法将参数(data.area)传递给 {{ }}
我必须这样做,以确认发布的是当前登录用户的专业知识之一。
解决方案
您有多种选择:
- 将数据传递到视图中并从 PHP 传递到 JS:
您在哪里调用您的视图:
use \App\Models\UserAreas;
// ...
$userAreas = UserAreas::where('user_id','=',Auth::id())->get();
return view('path/to/your/view', [ 'userAreas' => $userAreas ]);
在您的视野中:
var userAreas = @json($userAreas);
var channel = pusher.subscribe('question-channel');
channel.bind('question-event', function (data) {
if(userAreas.filter((area) => area.area_id === data.area).length)
var n = new Notification(data.title, {
icon: 'img/icon.png',
body: data.content,
image:'img/1.jpg'
});
n.onclick = function(e) {
e.preventDefault(); // prevent the browser from focusing the Notification's tab
window.open('http://127.0.0.1:8000/');
}
});
- 使用 API 端点
api.php
use \App\Models\UserAreas;
// ...
Route::get('user-areas/{{userArea}}', function(int $userArea) {
return (bool) UserAreas::where('user_id', Auth::id())->where('area_id', $userArea)->count();
});
your-view.blade.php
var channel = pusher.subscribe('question-channel');
channel.bind('question-event', function (data) {
fetch('api/user-areas/' + data.area).then(function(exists) {
if(exists) {
var n = new Notification(data.title, {
icon: 'img/icon.png',
body: data.content,
image:'img/1.jpg'
});
n.onclick = function(e) {
e.preventDefault(); // prevent the browser from focusing the Notification's tab
window.open('http://127.0.0.1:8000/');
}
}
}
});
推荐阅读
- c# - 为什么将 SelectedItem 作为自定义对象传递到新页面后没有内容显示?
- javascript - 使用 JavaScript 在后台播放 Spotify 客户端上的 Spotify 歌曲
- package-managers - LXC - 无法从 linux 容器访问包管理器
- javascript - 正文中的 HTML 代码
- bash - 这个 bash 命令有什么作用?
- r - 如何将一组 Unicode .txt 转换为 ANSI 以在 R 中进行文本分析
- css - CSS:圆形图像
- python - 在 Python 中绘制一个双条形图,其值来自两个不同的字典
- html - css不同的页面方向与一个打印过程
- c# - 无法在 Alea GPU (C#) 中启动内核