# Best approach for retrieving data from within a loop of posts with Blade?

**URL:** https://discourse.roots.io/t/best-approach-for-retrieving-data-from-within-a-loop-of-posts-with-blade/9972
**Category:** sage
**Created:** 2017-07-11T22:12:52Z
**Posts:** 4
**Showing post:** 4 of 4

## Post 4 by @Webstractions — 2017-10-25T18:29:58Z

The [ACF variables in blade template](https://discourse.roots.io/t/acf-variables-in-blade-template/10180) thread that @nathobson posted has lots of techniques. Good read.

You are on the right track. Plus there are probably a half-dozen ways to skin this cat, depends on your style. And sometimes there is just no way around throwing in a few `@php` directives here and there.

My preferences on using `@php` is to sometimes not use them.

```
// Instead of this
// <a href="@php echo get_the_permalink($post->ID) @endphp" class="button">Visit page</a>
<a href="<?= get_the_permalink($post->ID) ?>" class="button">Visit page</a>
```

Use the self-enclosed version of `@php` (note the absence of a semi-colon at end of statement)

```
// Instead of this
// @php $posts = $fields['quick_links']; @endphp
@php( $posts=$fields['quick_links'] )
```

And sometimes combine a `set` and `if` together

```
// Instead of this
// @php $posts = $fields['quick_links']; @endphp
// @if($posts)
@if( @php( $posts=$fields['quick_links'] ) )
```

---

_[View the full topic](https://discourse.roots.io/t/best-approach-for-retrieving-data-from-within-a-loop-of-posts-with-blade/9972)._
