Question

I'm trying to perform an .ajax get to populate a table using Knockout/Jquery in Laravel 4. I use Ardent and it keeps responding with the following json response.

{"throwOnFind":false}

Controller:

    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'));
}

JavaScript:

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());

I don't really know where to go from here. I think the problem may be in Ardent.

Was it helpful?

Solution

You are misusing the select method because the Role::select(...) only returns a query object (Builder) and not the result itself.

So need to use the other methods like get to execute the query and get the actual result.

So you need to write something liek:

$query = Role::select(array('roles.id',  ... , 'roles.created_at'));
$roles = $query->get();
return Response::json($roles, 200, array('Content-Type' => 'application/json'));
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top