Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

stefan7's avatar

@php in foreach

Hi, any idea why the $url-variable in between of @php is not working?

Exception: Trying to get property 'content' of non-object

               @foreach($position->url as $url)
                    @php
                        $content = $url->content;
                    @endphp
                @endforeach
0 likes
3 replies
Sergiu17's avatar

PHP is working,

This is what doesn't work

$url->content;
stefan7's avatar

Yes, but why? And how can i solve this? My editor shows my $url as undefined variable, but why can i not access it there within the loop?

Sergiu17's avatar

@STEFAN7 - debug

@foreach($position->url as $url)
    {{ dd($url) }} // is this an object? does it have `content` property?
@endforeach

Please or to participate in this conversation.