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 ?

2
  • change Response::json(array('data'=>$data)); into this return Response::json(array('data'=>$data));, might work. Commented Apr 24, 2015 at 10:49
  • how to show this with angular Commented Apr 24, 2015 at 11:05

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

Object of class Illuminate\Database\Eloquent\Collection could not be converted to int
now getting json array. but how to show this with angular

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.