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

if I use {{$node[0]->url}} then the Laravel templating engine will output the correct result, but I cannot figure out how to display everything using @for $ i = 0 in the @foreach loop, this is what I have in the 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 tried in the 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 
+5
source share
1 answer

The simple answer is: foreach in Blade works just like 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 the Blade syntax, but with the way you created your input variables. Given the way you create $oReturn in the code above, it will not have the properties you expect to expect. To illustrate here, a simplified version of what you are 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' ) ); 

will 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 will be an entry[] array that has one element, which is an array of nodes. From your input it is not clear that these nodes even have url elements. If you want to iterate over nodes, you need to do something like:

 @foreach ($oReturn->entry[0] as $node) <li>{{ $node->url }}</li> @endforeach 
It makes sense? I think you need to rethink your creation of $oReturn .

Update

The following is the feedback and output from your print_r statement, which should work:

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

(string) prints the result of $node->url to a string. Otherwise, PHP may consider it as a kind of object. SimpleXMLElement may be strange.

+10
source

Source: https://habr.com/ru/post/1235649/


All Articles