如何解决“ count():参数必须是数组或实现Countable的对象”对于PHP Laravel?

问题描述 投票:0回答:1

我想从数据库中检索所有人员总数详细信息。然后,在view.blade.php上查看详细信息。但是,当我运行系统时,在浏览器中弹出错误“ count():参数必须是数组或实现Countable的对象”。我的view.blade.php文件中有count()函数,用于统计接收该软件包的人数。我是否需要将count()的功能更改为其他功能,如果更改此功能,如何计算人数?

这是我的控制器

public function policyDownload(Request $request)
    {
        $package= Package::find($items["insurance_id"]);
        $items["package"] = $package;

        $headcounts = Plan::find($package["plan_id"]);
        $items["headcounts"] = $headcounts;

        return view('public.policy')->with($items);
    }

这是我的view.blade.php。

<h6><b>Additional Person Covered(s)</b></h6>
</b>
<b>
<h6>for Family Plan only</h6>
</b>
<br>
@if (count($headcounts) > 0 )
<table class="table table-bordered">
<thead>
<tr style="background-color:#d3d3d3">
<th scope="col">MyKad/MyKid/Passport No.</th>
<th scope="col">Fullname</th>
<th scope="col">Age</th>
</tr>
</thead>
<tbody>
@foreach ($headcounts as $item)
<tr>
<td>{{strtoupper($item['nric'])}}</td>
<td>{{strtoupper($item["name"])}}</td>
<td>{{strtoupper($item['age'])}}</td>
</tr>
@endforeach
</tbody>
</table>
<br><br>
@else
<br><br><br><br><br><br>
@endif
javascript php html mysql laravel
1个回答
0
投票

尝试一下使用!empty($headcounts)而不是count($headcounts) > 0


<h6><b>Additional Person Covered(s)</b></h6>
</b>
<b>
<h6>for Family Plan only</h6>
</b>
<br>
@if (!empty($headcounts))
<table class="table table-bordered">
<thead>
<tr style="background-color:#d3d3d3">
<th scope="col">MyKad/MyKid/Passport No.</th>
<th scope="col">Fullname</th>
<th scope="col">Age</th>
</tr>
</thead>
<tbody>
@foreach ($headcounts as $item)
<tr>
<td>{{strtoupper($item['nric'])}}</td>
<td>{{strtoupper($item["name"])}}</td>
<td>{{strtoupper($item['age'])}}</td>
</tr>
@endforeach
</tbody>
</table>
<br><br>
@else
<br><br><br><br><br><br>
@endif
热门问题
推荐问题
最新问题