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'));

这就是我在视图文件中try 的

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

抱歉,这是完整的打印结果

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

推荐答案

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

如果需要访问每个 node 的数组键值:

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

然而,我认为问题可能不在于您的Blade语法,而在于您创建输入变量的方式.考虑到您在上面的代码中创建$oReturn的方式,它将不会具有您所期望的属性.为了说明这一点,以下是您似乎正在创建的内容的简化版本:

// 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' ) );

将返回:

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

所以当你做@foreach ($oReturn as $node)的时候,$node的值就是entry[]数组,它有一个元素,也就是一个 node array.从您的输入中不清楚这些 node 甚至有url个元素.如果确实想在 node 之间循环,则必须执行以下操作:

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

这有意义吗?我认为你需要重新思考你创造的$oReturn.

更新

给出下面的反馈和上面print_r语句的输出,下面的方法应该是可行的:

@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.

Laravel相关问答推荐

拉威尔望远镜没有显示请求细节

使用Laravel Blade Formatter和PHP Intelephense进行 VSCode 格式化

如何在 Laravel 的外部 js 文件中包含 csrf_token()?

Laravel 错误ReflectionException-类 App\Http\Kernel 不存在

如何使用 Laravel Passport 在 API 中正确实现 OAuth?

Laravel 保存一对多关系

如何更改 Laravel5 中的视图文件夹?

如何从 laravel 控制器执行外部 shell 命令?

laravel 中的 Http Post 使用 fetch api 给出 TokenMismatchException

需要 Vagrant 环境或目标机器

Laravel excel maatwebsite 3.1 导入,excel 单元格中的日期列返回为未知格式数字.如何解决这个问题?

将 hasOne 模型附加到另一个 Laravel/Eloquent 模型而不指定 id

使用 laravel eloquent 关系检索除 NULL 之外的所有记录

为什么客户凭证应该与 Laravel Passport 中的用户相关联?

未找到列:1054 未知列 laravel

在 Laravel 中按用户名查找用户

如何在laravel中获取列值的平均值

在我的编辑器中管理上传文件的最佳方式?

将参数传递给 Laravel 中的中间件

laravel中的动态网址?