You have to make another array from your collection.
And then you will able to get anything that you need.
$result = [];
foreach ($users as $u) {
$result[ $u-> namecategory ] = $result[ $u-> namecategory ] ?? [];
$result[ $u-> namecategory ][$namesubcategory] = $result[ $u-> namecategory ][$namesubcategory] ?? [];
$result[ $u-> namecategory ][$namesubcategory][] = $u-> namesubling;
}
In result you will have an array like
result [mobile and assces]
[mobile]
[iphone]
[mobile cover]
[nexu cover]
Then you will be able to “foreach” this array like you want to.
Note please, that syntaxis “$a = $b[‘xx’] ?? [];” – PHP7
solved why nested loop not working in laravel