Saturday, March 4, 2017

How to echo variable which is inside Collection array?

I get some data from Controller and pass it to 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); 

In index view I did this

@foreach ($urls as $url)
<?php dd($url->analytics); ?>
@endforeach

so I get a Collection of some sort which is new to me.

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
    }
  ]
}

How do I get the values of each Analytic? I believe I need something like

$url->analytics->(enter_collection)[array_index]['Analytic']['quality'];

I always get two last Analytic values with each url so I would like to compare them



via Marko

Advertisement