Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel eloquent manipulate collection

I'm running a query which returns a collection array with 7 items -

$uniques = Analytics::fetchviews(Period::days(7))->take(7);

The collection results look like this-

  2 => array:4 [▼
  "date" => Carbon {#247 ▶}
  "uniquePageviews" => 1
  "pageViews" => 1
]

I would like to take out the pageViews and uniquePageviews and work with these integers before passing the results to a view. How can I edit the data within the collection directly?

like image 761
archvist Avatar asked Sep 08 '16 12:09

archvist


1 Answers

A collection is just a list of models. You can iterate over them, work with their attributes and so forth. For example:

foreach ($uniques as $unique) {
   var_dump($unique->pageViews);
}
like image 190
Elias Avatar answered Nov 18 '22 17:11

Elias