Laravel如何访问javascript文件中的身份验证。
在我的Laravel如何访问javascript文件中的身份验证。,javascript,php,laravel,Javascript,Php,Laravel,在我的@yield('scripts')部分中,有一个javascript调用,其中我仅在用户是管理员时显示一个按钮 我想把javascript放在我的库中,因为它被多次使用。但是,当我试图在库中包含此部分时,Blade/php不起作用myApp.js javascript部分 BootstrapDialog.show({ title: 'Age Not Met', message: 'The attendee does not meet the age reguirement for
@yield('scripts')
部分中,有一个javascript调用,其中我仅在用户是管理员时显示一个按钮
我想把javascript放在我的库中,因为它被多次使用。但是,当我试图在库中包含此部分时,Blade/php不起作用myApp.js
javascript部分
BootstrapDialog.show({
title: 'Age Not Met',
message: 'The attendee does not meet the age reguirement for this program.',
type: BootstrapDialog.TYPE_WARNING,
buttons: [
@if (Auth::user()->hasRole('admin')||Auth::user()->hasRole('programmer'))
{
label: 'OVERIDE',
cssClass: 'btn-danger',
action: function(dialogItself){
checkbox.prop('checked', true);
//...rest irrelevant
我试图在布局中使用一个变量来替换blade/php调用,但该变量为空。我还意识到我不能仅仅用if(isAdmin)
替换刀片,因为它在一个函数参数中…我如何才能让按钮仅为管理员显示
在布局中
<script>
var isAdmin = {{ (Auth::user()->hasRole('admin')||Auth::user()->hasRole('programmer')) }}
</script>
var isAdmin={{(Auth::user()->hasRole('admin')| | Auth::user()->hasRole('programmer')}
如果(isAdmin)正确,则使用的方法。您应该像在视图中那样定义它,然后:
var varButtons = []
if(isAdmin){
varButtons = [{
label: 'OVERIDE',
cssClass: 'btn-danger',
action: function(dialogItself){
checkbox.prop('checked', true);
}
}];
}
BootstrapDialog.show({
title: 'Age Not Met',
message: 'The attendee does not meet the age reguirement for this program.',
type: BootstrapDialog.TYPE_WARNING,
buttons: varButtons
如果您计划让javascript决定按钮是否显示,您可以让按钮始终可见,因为您的用户将能够以任何方式显示它。