$product=ProductCatalog::where(['campaign_id' => $campaign_id])->get();
print($product);
输出:
[{"id":7,"campaign_id":64,"product_id":26,"created_at":"2018-03-20 10:29:51","updated_at":"2018-03-20 10:29:51"},{"id":8,"campaign_id":64,"product_id":27,"created_at":"2018-03-20 10:30:26","updated_at":"2018-03-20 10:30:26"}]
从$ product我想获取product_id的值
如何从$ product中获取价值?
请帮我解决这个问题...
答案 0 :(得分:1)
您可以使用如下的toArray()
口才。
toArray
方法将集合转换为普通的PHParray
。如果集合的值为Eloquent
模型,则模型也将转换为数组。
$product = ProductCatalog::where(['campaign_id' => $campaign_id])->get()->toArray();
然后您可以根据需要访问数组属性。见下面的示例。
foreach($product as $productValue) {
print_r($productValue['product_id']);
}
答案 1 :(得分:0)
$productCatalogs = ProductCatalog::with('product')->where(['campaign_id' => $campaign_id])->get();
foreach($productCatalogs as $productCatalog){
print_r($productCatalog->product);
}
如果您在ProductCatalog Model(ProductCatalog.php)中定义了关系,那么这应该有用。