2016-05-16 86 views
2

我正在構建一個laravel應用程序,我想用ajax控制器生成一些報告。在MySQL中使用相同的原始查詢,我可以看到數據,但是使用ajax我無法在我的視圖頁面中顯示。 在控制檯它顯示[] No PropertiesPHP Laravel:Ajax無法檢索數據(無屬性)

enter image description here

我沒有得到爲什麼它表示對上述錯誤。如果有人發現有什麼問題,請幫我找出答案。謝謝。

這裏是我用於從數據庫中檢索數據的控制器:

public function ajax_view_schedule(Request $request) 
    { 

      $dept_id= $request->Input(['dept_id']); 
$schedule= DB::select(DB::raw("SELECT courses.code as c_code, courses.name as c_name,COALESCE(CONCAT('R. No',':',rooms.room_number,', ',days.name ,', ', allocate_rooms.start,' - ',allocate_rooms.end),'Not Scheduled Yet') AS schedule 
FROM departments join courses on departments.id = courses.department_id 
left join allocate_rooms on allocate_rooms.course_id=courses.id 
left join rooms on allocate_rooms.room_id=rooms.id 
left join days on allocate_rooms.day_id=days.id WHERE departments.id='.$dept_id.'")); 
     return \Response::json($schedule); 
    } 

這裏與Ajax代碼視圖頁面:

<div class="container" > 
     <h3> View Class Schedule and Room Allocation Information </h3> 

    <div class="form-group"> 
     <label for="">Department</label> 
     <select class="form-control input-sm" required id="department" name="department_id" > 
     <option>Select a Department</option> 
     @foreach($department as $row) 
     <option value="{{$row->id}}">{{$row->name}}</option> 
     @endforeach 
     </select> 
    </div> 



    <table class="table table-striped table-bordered" id="example"> 
    <thead> 
     <tr> 

     <td>Course Code</td> 
     <td>Name</td> 
     <td>Schedule Info</td>      
     </tr> 
    </thead> 
    <tbody> 

    </tbody> 
    </table>   
    </div> 

    <script type="text/javascript"> 
    $('#department').on('change',function(e){    
     var dept_id = $('#department option:selected').attr('value'); 

     $.ajaxSetup({ 
        headers: { 
         'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content') 
        } 
        }); 

       $.ajax({ 
        type: "POST", 
        url : "{{url('ajax-view-schedule')}}", 
        data:{dept_id:dept_id}, 
       success : function(data) { 
         var $tbody = $('#example tbody').empty();     
        $.each(data,function(index,subcatObj){      
        $tbody.append('<tr><td class="code">' + subcatObj.c_code + '</td><td class="course_name">' + subcatObj.c_name + '</td><td class="schedule">' + subcatObj.schedule + '</td></tr>'); 
         }); 
        } 
       });  
     }); 
    </script> 
+0

你可以發佈開發人員工具的網絡屏幕截圖? –

+0

添加了qus。 – User57

+0

當您導航到website.url/ajax-view-schedule時,您會看到什麼? –

回答

1

你實際上是在雙引號這樣的PHP將插入你的變量

public function ajax_view_schedule(Request $request) 
    { 

      $dept_id= $request->Input(['dept_id']); 
$schedule= DB::select(DB::raw("SELECT courses.code as c_code, courses.name as c_name,COALESCE(CONCAT('R. No',':',rooms.room_number,', ',days.name ,', ', allocate_rooms.start,' - ',allocate_rooms.end),'Not Scheduled Yet') AS schedule 
FROM departments join courses on departments.id = courses.department_id 
left join allocate_rooms on allocate_rooms.course_id=courses.id 
left join rooms on allocate_rooms.room_id=rooms.id 
left join days on allocate_rooms.day_id=days.id WHERE departments.id='$dept_id'"));//remove dot from .$dept_id. 
     return \Response::json($schedule); 
    } 
+0

感謝您的時間。得到了錯誤! :) – User57