Laravel正确解析json输出

时间:2015-11-22 00:15:35

标签: json

我在laravel中使用此代码。

 $user_detail = \App\Userpacakage::with('user.pacakage')->where('pacakage_id', '=', 1)->get(); //->lists('nombre', 'id')
        return response()->json($user_detail);

这是我的json输出 - >

[{"id":1,"user_id":1,"pacakage_id":1,"transaction_id":"jfjkgjgjkhkjkg55rtf","created_at":"-0001-11-30 00:00:00","updated_at":"-0001-11-30 00:00:00","user":{"id":1,"name":"omsun","email":"omsun123@gmail.com","contact":2147483647,"regkey":"","created_at":"2015-11-21 01:18:33","updated_at":"2015-11-21 01:18:33","pacakage":[{"id":1,"title":"dsagdjas","description":"<p>\r\n\tsamndbmasndmas,<\/p>\r\n","tab_id":0,"from":"11\/27\/2015","to":"12\/03\/2015","days":22,"unlimited":0,"criteria":1,"category_id":1,"subcategory_id":0,"subject_id":0,"price":2432,"created_at":"2015-11-21 19:07:10","updated_at":"2015-11-21 19:07:10","pivot":{"user_id":1,"pacakage_id":1}}]}}]

使用jquery我正在使用此代码来获取用户详细信息

$.get('api/classallocation?pacakage_id=' + pacakage_id, function (data) {

                    console.log(data);
                     $.each(data, function (i, user) {

                         console.log(user.name));
                     });

我想知道如何在jquery中获取pacakage详细信息。比如pacakage title,price。

1 个答案:

答案 0 :(得分:1)

$.get('api/classallocation?pacakage_id=' + pacakage_id, function (data) {
   console.log(data);
   $.each(data, function (i, user) {
   console.log(user.pacakage[0].title));
});