I haven’t tested this inside the loop, but you can return anonymous functions as variables and then execute them in your blades. Since iirc the loop relies on global state they should work. i.e.:
// SomeComposer.php
public function with()
{
return [
'cat_links' => function() {
$cats = get_the_category();
$output = "";
foreach ($cats as $cat) {
$output .= get_category_link($cat->slug);
}
return $output;
},
];
}
// some-view.blade.php
{{ $cat_links() }}