2
In the image below we see a typical example of Collection in Laravel. We may see in Relations there is another Collection.
How do I access this in the View?
I’m doing so in Controller:
$users = User::with(['logs' => function($q) use ($startAt, $endAt){
$q->whereDate('created_at', '>=', $startAt->format('Y-m-d H:i:s'));
$q->whereDate('created_at', '<=', $endAt->format('Y-m-d H:i:s'));
}]);
I know at View I can make one:
@foreach($users as $user)
@endforeach
The question is how can I get the Collection 'Logs' inside the Foreach for that user who is passing there.
Groupby and Filter
$videoList = $user['logs']->filter(function($q){
return $q->content->type == 'video';
})->groupBy('content_id');
You cannot pull the $users['Relations']['logs'] and loop from there ?
– AnthraxisBR
I can. But I’m using the same object for several things. But I’m going to test the suggestions. I haven’t touched Lavarel for a while...
– Diego Souza
loopa as you did, take the array_key_exist (or some similar in the Laravel), and if you hit the key with loopa Relations again inside the already open loop to get the logs#items, then you do not need to open separate, but end up doing a more check
– AnthraxisBR