2017-03-16 61 views
1

我知道如何将刀片模板中的变量传递给jQuery模态,但我不知道如何以其他方式做到这一点。我可以检查用户是否例如与注册:从jQuery到刀片变量

@if(\Illuminate\Support\Facades\Auth::user()) 
    open modal with options 
@else 
    open basic modal message "you are not authorized" 
@endif 

但现在,我想检查是否注册的用户可使用上,他点击的设备。我有所有必要的模型关系,所以我可以做一些像Auth::user()->equipment->where('equipment_id', $equipment->id) != null ...但问题是,我可以将一个变量转发给像data-equipment-id = ...这样的jQuery并在脚本中获取它......但是我怎样才能将它返回到PHP变量,这样我可以访问它想:

@if(\Illuminate\Support\Facades\Auth::user()) 
    @if(Auth::user()->equipment->where('equipment_id', $equipment->id) != null) 
     allowed! 
    @else 
     not allowed! 
    @endif 
@else 
    open basic modal message "you are not authorized" 
@endif 

编辑:

这是我如何将数据转发到模态:

@foreach($equipment as $instrument) 
    <td> 
     <a href="#" style="display: block;" 
      data-href="{{route('reserve-me', [$instrument->id, $scheduler_start_time->timestamp])}}" 
      data-toggle="modal" 
      data-target="#confirm-reservation" 
      data-start-time="{{$scheduler_start_time->toW3cString()}}" 
      data-end-time="{{$scheduler_end_time->toW3cString()}}" 
      > 

      @if($instrument->reservations 
      ->where('reserved_from','<=', $scheduler_start_time) 
      ->where('reserved_to','>=', $scheduler_start_time)->first() != null) 

       HERE 

      @else 
       &nbsp; 
      @endif 
     </a> 
    </td> 
@endforeach 

然后模态脚本:

$('#confirm-reservation').on('show.bs.modal', function (e) { 
    $(this).find('.dropdown-menu li').remove(); 
    $(this).find('.btn-ok').attr('href', $(e.relatedTarget).data('href')); 
    var startTime = moment($(e.relatedTarget).data('start-time')).utc(); 
    var endTime = moment($(e.relatedTarget).data('end-time')).utc(); 

    while (startTime < endTime) { 
     $(this).find('.dropdown-menu').append('<li><a href="#">' + startTime.format("HH:mm") + '</a></li>'); 
     startTime.add(30, 'minutes'); 
    } 

    $(document).on('click', '#confirm-reservation .dropdown-menu a', function() { 
     $('#insert_text').text($(this).text()); 
     var href = $(e.relatedTarget).data('href'); 
     var time = moment().year(startTime.format("Y")).month(startTime.format("M")).date(startTime.format("D")) 
       .hour(($(this).text().split(':')[0]) - 2).minutes($(this).text().split(':')[1]).seconds("0"); 

     console.log(time); 

     $('.btn-ok').attr('href', href + '/' + time.unix()); 
    }); 
}); 
+1

你可以用'AJAX' http://api.jquery.com/jquery.ajax/ – Ionut

回答

0

使用jQuery和Ajax实现这一目标,如:

$.ajax({ 
    url: 'your route',       
    type: 'POST',         
    data: { 
     var1 :val1, 
    }, 
    success: function(response){ 
     // response from controller function which contains a message in it 
     // pass it to modal or dialog like 

     $('#dialog').html(response); 
     $('#dialog').dialog(); 

    } 
}); 
+0

谢谢你的答案..我用更多的代码更新了我的问题,你可能更具体一些吗?我只能将变量推送到模态脚本 – Norgul