首页 > 解决方案 > Laravel 脚本中遇到的非数字值

问题描述

在页面上的我的 Laravel 脚本中,出现此错误:

“遇到非数字值(查看:/home/grammer/public_html/test/core/resources/views/admin/apiServices.blade.php)”

对于这条线

<td>{{ $key+1 }}</td>

我的页面代码:

@extends('admin.layouts.master')
@section('page_icon', 'fa fa-suitcase')
@section('page_name', 'API Services')
@section('body')
    <div class="row">
        <div class="col-md-12">
            <div class="tile">
                <h3 class="tile-title">API Services List</h3>
                <table class="table table-hover">
                    <thead>
                    <tr>
                        <th>Serial</th>
                        <th>Service ID</th>
                        <th>Name</th>
                        <th>Category</th>
                        <th>Price</th>
                        <th>Min</th>
                        <th>Max</th>
                    </tr>
                    </thead>
                    <tbody>
                    @foreach($items as $key => $item)
                        <tr>
                            <td>{{ $key+1 }}</td>
                            <td>{{ isset($item->service->id) ? $item->service->id : $item->service}}</td>
                            <td>{{ $item->name }}</td>
                            <td>{{ $item->category }}</td>
                            <td>{{ isset($item->rate) ? $item->rate : $item->price_per_k }} $</td>
                            <td>{{ $item->min }}</td>
                            <td>{{ $item->max }}</td>
                        </tr>
                    @endforeach
                    </tbody>
                </table>
            </div>
        </div>
    </div>
@endsection

标签: phplaravellaravel-5

解决方案


只需添加:

<td>{{ (int) $key + 1 }}</td>

添加:

$key = (int) $key; // GET NUMBER FROM VARIABLE

推荐阅读