我编写了一个简单的代码来打印出关联数组的元素,但它是在条件循环中,尽管我认为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
答案 0 :(得分:1)
我想你应该试试这个:
@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
注意:您的$ pms可能不是数组OR对象