Trying to get key in a foreach loop to work using blade

Artful_dodger picture Artful_dodger · Nov 11, 2015 · Viewed 63.9k times · Source

if I use {{$node[0]->url}} then Laravel's templating engine dislays the correct result but I can not figure out how to display all using the @for $i=0 within a @foreach loop this is what I have in my routes file

$oReturn = new stdClass();
        $fid='endpoints';//sample fid

        $url = 'http://localhost:5493/sdata/$system/registry/'.$fid;

        $xml = simplexml_load_file($url);
        foreach($xml->xpath("//sdata:payload") as $entry) {
            // xpath here must be from payload to endPoint--type
            $content = $entry->xpath("./sdata:endPoint--type");

            foreach($content as $c) {
                // Make set of children with prefix sdata
                $nodes = $c->children('sdata', true);

            }

// add parsed data to the array
            $oReturn->entry[] = $nodes;

        }
        return View::make('index', compact('oReturn'));

and this is what I have tried in my view file

@for($i=0; $i < 4; $i++)
@endfor
@foreach ($oReturn as $node)
   <li>{{$node[$i]->url}}</li>
@endforeach

sorry here is the complete print_r result

Array ( [oReturn] => stdClass Object 
( [entry] => Array 
    ( 
        [0] => SimpleXMLElement Object ( [description] => Sage 50 Accounts [protocol] => http [host] => base_3 [applicationName] => accounts50 [contractName] => SimpleXMLElement Object ( ) [dataSetName] => - [url] => http://base_3:5493/sdata/accounts50 [isBrowsable] => true [aliveStamp] => 2015-11-06T23:31:10.031+00:00 ) 
        [1] => SimpleXMLElement Object ( [endPointType] => dataSet [applicationName] => accounts50 [contractName] => GCRM [dataSetName] => Enter Your Company Name [url] => http://base_3:5493/sdata/accounts50/GCRM/{C22ACA13-3C4C-4E33-A584-CD99BD3002A6} ) 
        [2] => SimpleXMLElement Object ( [endPointType] => dataSet [applicationName] => accounts50 [contractName] => GCRM [dataSetName] => Enter Your Company Name [url] => http://base_3:5493/sdata/accounts50/GCRM/{FF476636-D4AF-4191-BDE4-891EDA349A68} ) 
        [3] => SimpleXMLElement Object ( [endPointType] => dataSet [applicationName] => accounts50 [contractName] => GCRM [dataSetName] => Enter Your Company Name [url] => http://base_3:5493/sdata/accounts50/GCRM/{C62A13D5-3FFE-43B4-9DAF-38F9055A83C7} ) 
        [4] => SimpleXMLElement Object ( [description] => GCRM Contract [endPointType] => contract [protocol] => http [host] => base_3 [applicationName] => accounts50 [contractName] => GCRM [dataSetName] => - [url] => http://base_3:5493/sdata/accounts50/GCRM [aliveStamp] => 2015-11-06T23:31:11.062+00:00 ) 
    ) 
) 
) 1

Answer

morphatic picture morphatic · Nov 11, 2015

The simple answer is that foreach in Blade works the same as a regular PHP foreach. You should be able to do something like:

@foreach ($nodes as $node)
    <li>{{ $node->url }}</li>
@endforeach

If you need access to the array key value for each node:

@foreach ($nodes as $key => $node)
    <li>{{ $key }}: {{ $node->url }}</li>
@endforeach

However, I think the problem may not be with your Blade syntax, but with the way you created your input variables. Given the way you created $oReturn in the code above, it won't have the properties you appear to be expecting. To illustrate, here's a simplified version of what you appear to be creating:

// initialize your return variable 
$oReturn = new stdClass();

// create a dummy array <sdata:x> nodes,
// to simulate $nodes = $c->children('sdata', true);
$node = new SimpleXMLElement('<sdata:x/>');
$nodes = [ $node, $node, $node ];

// simulate adding nodes to the array of entries 
$oReturn->entry[] = [ $node, $node, $node ];

// print out the resulting structure
print_r( compact( 'oReturn' ) );

would return:

Array(
    [oReturn] => stdClass Object
        (
            [entry] => Array
                (
                    [0] => Array
                        (
                            [0] => SimpleXMLElement Object()
                            [1] => SimpleXMLElement Object()
                            [2] => SimpleXMLElement Object()
                        )
                )
        )
)

So when you do @foreach ($oReturn as $node) the value of $node would be the entry[] array, which has a single element, that is an array of nodes. It is not clear from your input that these nodes even have url elements. If you did want to loop through the nodes you'd have to do something like:

@foreach ($oReturn->entry[0] as $node)
    <li>{{ $node->url }}</li>
@endforeach

Does this make sense? I think you need to rethink your creation of $oReturn.

Update

Given feedback below and the output of your print_r statement above, the following should work:

@foreach ($oReturn->entry as $node)
    <li>{{ (string) $node->url }}</li>
@endforeach

The (string) casts the result of $node->url to string. Otherwise PHP may treat it as some kind of object. SimpleXMLElement can be weird.