我从Controller获取一些数据并将其传递给View with
// Foreach url get latest 2 Analytics data - This isn't my code but it works
$urls = URL::with('analytics')->get()->map(function($urls) {
$urls->analytics = $urls->analytics->sortByDesc('created_at')->take(2);
return $urls;
});
return view('urls.index')->with('urls', $urls);
在索引视图中我做了这个
@foreach ($urls as $url)
<?php dd($url->analytics); ?>
@endforeach
所以我得到了一些对我来说很新的收藏品。
Collection {#422 ▼
#items: array:2 [▼
12 => Analytic {#2076 ▶}
11 => Analytic {#1951 ▼
#table: "analytics"
#connection: null
#primaryKey: "id"
#keyType: "int"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:5 [▼
"id" => 990
"quality" => 1
"url_id" => 23
"created_at" => "2017-02-20 07:00:02"
"updated_at" => "2017-02-20 07:00:02"
]
#original: array:5 [▶]
#relations: []
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [▶]
#dates: []
#dateFormat: null
#casts: []
#touches: []
#observables: []
#with: []
+exists: true
+wasRecentlyCreated: false
}
]
}
如何获取每个分析的值? 我相信我需要像
这样的东西$url->analytics->(enter_collection)[array_index]['Analytic']['quality'];
我总是在每个网址上得到最后两个分析值,所以我想比较它们
答案 0 :(得分:1)
看起来您需要仔细查看Collections文档!在您的特定情况下,由于您只使用其中的2个,您可以执行以下操作:
$url->analytics->first()->quality
$url->analytics->last()->quality
但是,集合确实会实现ArrayAccess
,因此您还可以执行$url->analytics[index]->quality
之类的操作。