Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel 在blade中使用foreach打印关联数组时出现的问题_Laravel_Loops_Associative Array_Blade - Fatal编程技术网

Laravel 在blade中使用foreach打印关联数组时出现的问题

Laravel 在blade中使用foreach打印关联数组时出现的问题,laravel,loops,associative-array,blade,Laravel,Loops,Associative Array,Blade,我已经编写了一个简单的代码来打印出关联数组的元素,但它是在一个条件循环中,尽管我认为foreach的编写是正确的。它不起作用。foreach循环正在使用一个通过路由传递的变量 return view('results') ->with('name', $name) ->with('state', $state) ->with('pms', $pms) ->with('hasresult', $hasresult) ->with

我已经编写了一个简单的代码来打印出关联数组的元素,但它是在一个条件循环中,尽管我认为foreach的编写是正确的。它不起作用。foreach循环正在使用一个通过路由传递的变量

return view('results')
    ->with('name', $name)
    ->with('state', $state)
    ->with('pms', $pms)
    ->with('hasresult', $hasresult)
    ->with('err', $err)
    ->with('errNoEntries', $errNoEntries);
代码如下所示:

@extends('layouts.master')

@section('title')
    Search Results
@endsection

@section('content')

    <h2>Australian Prime Ministers</h2>

    @if ($errNoEntries) {{-- meaning the data was entered incorrectly --}}
        <p class='alert'>{{$err}}</p>
    @elseif (!$hasresult)
        <p class='alert'>No Results found</p>
    @else
        <table class="bordered">
            <thead>
                <tr>
                    <th>No.</th>
                    <th>Name</th>
                    <th>From</th>
                </tr>
            </thead>
            <tbody>
                @foreach($pms as $pm) 
                    <tr>
                        <td>{{$pm['index']}}</td>
                        <td>{{$pm['name']}}</td>
                        <td>{{$pm['from']}}</td>
                    </tr>
                @endforeach
            </tbody>
        </table>
    @endif

    <form method="post" action="searchresult">
        {{csrf_field()}}
        <table>
            <tr><td>Name: </td><td><input type="text" name="name"></td></tr>
            <tr><td>Year: </td><td><input type="text" name="year"></td></tr>
            <tr><td>State: </td><td><input type="text" name="state"></td></tr>
            <tr><td colspan=2><input type="submit" value="Search">
            <input type="reset" value="Reset"></td></tr>
         <table>
     </form>

@endsection
@extends('layouts.master'))
@章节(“标题”)
搜索结果
@端部
@节(“内容”)
澳大利亚总理
@如果($errNoEntries){{--表示数据输入不正确--}

{{$err}

@elseif(!$hasresult)

未找到任何结果

@否则 不 名称 从…起 @foreach($pms作为$pm) {{$pm['index']} {{$pm['name']} {{$pm['from']} @endforeach @恩迪夫 {{csrf_field()}} 姓名: 年份: 声明: @端部
我想你应该试试这个:

@if(is_object($pms) || is_array($pms))
@foreach($pms as $pm) 
              <tr><td>{{$pm['index']}}</td><td>{{$pm['name']}}</td><td>{{$pm['from']}}</td></tr>
            @endforeach

@endif
@if(is_对象($pms)| is_数组($pms))
@foreach($pms作为$pm)
{{$pm['index']}{{$pm['name']}}{{$pm['from']}}
@endforeach
@恩迪夫

注意:可能是您的$pms不是数组或对象

给出了哪个错误?$err的输出是什么?ErrorException为foreach()提供的参数无效(View:/home/ubuntu/workspace/WebAppDev/Week4/assoc laravel/resources/views/results.blade.php)err是字符串类型的变量。如果elseif工作正常,错误检查工作正常,那么最上面的就是else位,特别是关联数组的打印。$pms数组中的典型元素=$pms=数组(数组('index'=>'1','name'=>'Edmund Barton','from'=>'1901年1月1日','to'=>'1903年9月24日','party'=>'protectism','duration'=>'2年8个月24天','state'=>'新南威尔士州');谢谢虽然我不只是调试方法,但我调试后发现,由于一个逻辑错误,我的数组实际上是空的。代码是正确的