2013-10-14 1 views
1

Laravel 4에서 Knockout/Jquery를 사용하여 테이블을 채우려는 .ajax get을 수행하려고합니다. Ardent를 사용하고 다음 json 응답에 응답합니다.throwOnFind : 거짓 응답을 Laravel의 Ardent에서 가져 오기

{"throwOnFind":false} 

컨트롤러 :

public function getData() 
{ 
    $roles = Role::select(array('roles.id', 'roles.name', 'roles.id as users', 'roles.created_at')); 
    return Response::json($roles, 200, array('Content-Type' => 'application/json')); 
} 

자바 스크립트는 :

function Role(data) { 
    this.id = ko.observable(data.id); 
    this.name = ko.observable(data.name); 
    this.users = ko.observable(data.users); 
    this.created_at = ko.observable(data.created_at); 
} 
function ViewModel() { 
    var self = this; 
    self.roles = ko.observableArray([]); 
    $.ajax({ 
     type: "GET", 
     url: "{{ URL::to('admin/roles/data') }}", 
     complete: function(allData) { 
      var mappedRoles = $.map(allData, function(item) { 
       return new Role(item); 
      }); 
     } 
    }, "json"); 

    self.roles(mappedRoles); 
} 

ko.applyBindings(new ViewModel()); 

정말 당장은 모른다. 나는이 문제가 열렬히 일어날 수 있다고 생각한다.

답변

2

Role::select(...)은 결과 자체가 아닌 쿼리 개체 (Builder) 만 반환하기 때문에 select 메서드를 잘못 사용하고 있습니다.

쿼리를 실행하고 실제 결과를 얻으려면 get과 같은 다른 방법을 사용해야합니다. 그래서 간단한

$query = Role::select(array('roles.id', ... , 'roles.created_at')); 
$roles = $query->get(); 
return Response::json($roles, 200, array('Content-Type' => 'application/json')); 
+0

:

그래서 당신은 뭔가 liek을 작성해야합니다. 또한 이것을 표시하기 위해 Knockout 용 매핑 플러그인을 사용해야했습니다. – Chris

관련 문제