访问数组中的数据

时间:2018-04-16 11:15:13

标签: php laravel

我从我的数据库中检索数据,如下所示和数组响应。我正在尝试访问产品并显示 pids 。如何做到这一点。 我从我的数据库中检索数据,如下所示和数组响应。我正在尝试访问产品并显示 pids 。如何做到这一点。

PS:初学者用laravel。请帮忙

$amazon_products = Group::where('id',$auth_data)->with('products')->get();

return $amazon_products



[  
   {  
      "id":1,
      "title":"Shipment",
      "id":1,
      "created_at":"2018-04-15 23:55:00",
      "updated_at":"2018-04-16 00:10:52",
      "products":[  
         {  
            "id":3,
            "name":"MacBook",
            "pid":"050",
            "group_id":null,
            "user_id":1,
            "created_at":"2018-04-16 11:06:08",
            "updated_at":"2018-04-16 11:06:08",
            "pivot":{  
               "category_id":1,
               "product_id":3,
               "created_at":"2018-04-16 11:06:08",
               "updated_at":"2018-04-16 11:06:08"
            }
         }
      ]
   },
   {  
      "id":2,
      "title":"Gaming",
      "id":1,
      "created_at":"2018-04-15 23:55:30",
      "updated_at":"2018-04-15 23:55:30",
      "products":[  
         {  
            "id":1,
            "name":"Samsung",
            "pid":"024",
            "group_id":null,
            "user_id":1,
            "created_at":"2018-04-16 00:14:20",
            "updated_at":"2018-04-16 05:31:05",
            "pivot":{  
               "group_id":2,
               "customer_id":1,
               "created_at":"2018-04-16 05:33:08",
               "updated_at":"2018-04-16 05:33:08"
            }
         }
      ]
   }
]

以下是我的小组的模型

 public function products()
    {
        return $this->belongsToMany('App\Product','group_product','group_id','product_id')
        ->withTimestamps();
    }

1 个答案:

答案 0 :(得分:0)

在急切加载产品后列出产品pids:

...->get()->load('products')->lists('products.pid');