D
D
dk-web2016-04-18 20:46:23
Angular
dk-web, 2016-04-18 20:46:23

Passing data laravel->angular?

Guys, sorry ... blunted.

return [
          'sources' => Source::where('user_id', $request->user()->id)->get()->toJson(),
      ];

I want to do this in Angular...
Order.get()
        .success(function(data) {
console.log(data);
            $scope.sources = data.sources;
console.log($scope.sources);

        });

apparently it turns out a string, not an array of objects, because angular swears
angular.js:12416 Error: [ngRepeat:dupes] errors.angularjs.org/1.4.5/ngRepeat/dupes?p0=sourc...
well, so as not to produce questions... .final task from laravel to pass json array
return [
          'sources' => Source::where('user_id', $request->user()->id)->get()->toJson(),
          'types' => [{'id'=>1,'type='нал'},{'id'=>'2','type'=>'карта'] 
      ];

and accordingly in Angular get...
Order.get()
        .success(function(data) {
console.log(data);
            $scope.sources = data.sources;
            $scope.types = data.types;
console.log($scope.sources);

        });

then ng-repeat="source in sources"... type in types.
what am I doing wrong? need support

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vyacheslav Plisko, 2016-04-19
@AmdY

You will have a line in your sources, it is clear that there will be problems with ng-repeat.
In types, you also have heresy.
No need to pull toJSON or form it manually. just give an object and an array

return [
          'sources' => Source::where('user_id', $request->user()->id)->first(),
          'types' => [
                  ['id'=>1,  'type' => 'нал'],
                  ['id'=> 2, 'type' => 'карта']
           ] 
      ];

S
Sergey, 2016-04-18
Protko @Fesor

apparently it turns out a string, not an array of objects, because the angular swears

1) make sure that valid JSON is coming from the server
2) check that valid JSON is coming from the server and the Content-Type: application/json header.

V
Valeriy Donika, 2016-04-19
@Valonix

There, in the documentation, it seems to be written that when returning without a view, json is given, that is, you don’t need to change it already. The guys thought this moment. Look in the Responses section just in case https://laravel.com/docs/5.2/responses#json-responses

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question