why can't access property in eager loaded senario?
i trying access photo location facilities model. have many:many relationship. when load facility info using
$facilities = facility::with('photos')->get(); when try access
foreach($facilities $facility) { echo $facility->photos->id; } i undefined property: illuminate\database\eloquent\collection::$id
if echo $facilities->photos end with.
[{"id":"3", "name":null, "location":"facilities\/facility1.jpg\r", "created_at":"2013-07-18 14:15:19", "deleted_at":null, "updated_at":null, "pivot":{"facility_id":"5","photo_id":"3"}}] whats best way access property in array?
with eager loading photos photos collection , in order photo need iterate through photos collection e.g.
foreach($facilities $facility) { foreach($facility->photos $photo) { echo $photo->id; } } if want first photo can calling first() method on collection
foreach($facilities $facility) { $facility->photos->first()->id; }
Comments
Post a Comment