2

I tried to Bind Laravel Eloquent ORM array to Angular JS View.

This is my code,

controller (Laravel)

public function index()
{
    $data = User::all();

    Response::json(array('data'=>$data));
}

View (Laravel)

<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Laravel PHP Framework</title>
<script type="text/javascript" href="{{URL::asset('js/angular.js')}}"></script>
<script type="text/javascript"  href="{{URL::asset('js/controller.js')}}"></script>
</head>
<body>
<div class="welcome">
    <ul ng-controller="clientList">
        <li ng-repeat="clients as clients">{{clients.name}}</li>
    </ul>

</div>

Controller (Anular JS)

var anguApp = angular.module('anguApp', []);

anguApp.controller('clientList',function($scope,$http)
{

   $scope.clients=[];

   $http.get('/').success(function (data, status) {
   $scope.clients = data;
   console.log(data);
});
});

Now getting a blank screen in browser.... How to solve this ?

Jishad
  • 2,375
  • 5
  • 25
  • 55

1 Answers1

1

I'm fairly certain you shouldn't wrap the results of User::all(); into an array. That is, it is enough to do the pass the results of it directly:

Response::json($data->toArray());

Also, you need to return whatever the Laravel controller does so:

public function index()
{
    $users = User::all();

    return Response::json($users->toArray());
}

Update:

Or, following Returning an Eloquent model as JSON in Laravel 4, you can also return the model itself so:

public function index()
{
    $users = User::all();

    return $users;
}

Update 2:

Pretty sure your AngularJS syntax is wrong, according to the docs it is:

<div ng-repeat="(key, value) in myObj"> ... </div>

So your code should read:

<ul ng-controller="clientList">
    <li ng-repeat="client in clients">{{client.name}}</li>
</ul>
Community
  • 1
  • 1
Crembo
  • 4,098
  • 3
  • 23
  • 29