PHP Laravel:Ajax无法检索数据(无属性)

时间:2016-05-16 05:52:05

标签: php mysql ajax laravel laravel-5

我正在构建一个laravel应用程序,我想用ajax控制器生成一些报告。在MySQL中使用相同的原始查询,我可以看到数据,但使用ajax我无法在我的视图页面中显示。 在控制台中显示[] No Properties

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>   

1 个答案:

答案 0 :(得分: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);  
    }