2017-03-16 79 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