【问题标题】:empty() function returns false although array contains empty array尽管数组包含空数组,但 empty() 函数返回 false
【发布时间】:2020-03-18 12:32:27
【问题描述】:

我不确定为什么尽管没有与雪兰莪匹配的任何数据,但未显示所需的“未找到记录”行

BazaarLocController:

public function index()
    {
        $bzr = BzrLocation::where('area', 'Perlis')->get();
        $bzr2 = BzrLocation::where('area', 'Selangor')->get();
        $bzr3 = BzrLocation::where('area', 'Pahang')->get();
        $bzr4 = BzrLocation::where('area', 'Johor')->get();
        $bzr5 = BzrLocation::where('area', 'Melaka')->get();
        $bzr6 = BzrLocation::where('area', 'Negeri Sembilan')->get();

    $allbzr = [
        'bzrloc' => $bzr,
        'bzrloc2' => $bzr2,
        'bzrloc3' => $bzr3,
        'bzrloc4' => $bzr4,
        'bzrloc5' => $bzr5,
        'bzrloc6' => $bzr6
    ];

    return view('admin.bazaarloc')->with($allbzr);
   }

bazaarloc.blade.php:

<table class="table table-hover">
   <caption>Table 2</caption>
   <thead class="thead-dark">
     <tr>
       <th scope="col" style="width: 8%; vertical-align: top;">No.</th>
       <th scope="col" style="width: 60%; vertical-align: top;">Location</th>
       <th scope="col" style="width: 21%; vertical-align: top;">Amount Approved</th>
       <th scope="col" style="width: 21%; vertical-align: top;">Description</th>
       <th scope="col" style="width: 4%;"></th>
       <th scope="col" style="width: 4%;"></th>
     </tr>
   </thead>
   <tbody>
     @php
     $counting = 0
     @endphp
     @foreach($bzrloc2 as $bzr)
     @php
     $counting++
     @endphp
     <tr>
       <th scope="row">{{$counting}}</th>
       <td>{{$bzr->name}}</td>
       <td>{{$bzr->totalPlot}}</td>
       <td>{{$bzr->plotAvail}}</td>
       <td><a href="{{ route('bazaarLocation.edit', $bzr->bzrloc_id) }}"><button type="submit" class="btn btn-success btn-sm">Edit</button></a></td>
       <td>
         <form method="POST" action="{{ route('bazaarLocation.destroy', $bzr->bzrloc_id) }}">
             @csrf
             @method('DELETE')
             <button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('Adakah anda pasti ingin membatalkan lokasi tersebut?')">Batal</button>
         </form>
       </td>
     </tr>
     @endforeach
     @if(empty($bzrloc2))
     <tr>
       <td colspan="2">
         No record found
       </td>
     </tr>
     @endif
   </tbody>
 </table>

当打印出 $bzrloc2 时,它显示 []。这确认数组是空的。根据php手册,empty()函数应该认为这个为空并返回true。

【问题讨论】:

  • 打印到 $allbzr 并检查数据是否来自数据库?
  • 你是不是又错过了一些@php?
  • @dagelf 不,完整
  • @PHPGeek 这个问题已经解决了:)

标签: php laravel


【解决方案1】:

因为你使用的是集合,而不是数组。收藏将返回false

empty(collect([]))  // false
empty([])           // true

试试下面的代码:

<table class="table table-hover">
    <caption>Table 2</caption>
    <thead class="thead-dark">
        <tr>
            <th scope="col" style="width: 8%; vertical-align: top;">No.</th>
            <th scope="col" style="width: 60%; vertical-align: top;">Location</th>
            <th scope="col" style="width: 21%; vertical-align: top;">Amount Approved</th>
            <th scope="col" style="width: 21%; vertical-align: top;">Description</th>
            <th scope="col" style="width: 4%;"></th>
            <th scope="col" style="width: 4%;"></th>
        </tr>
    </thead>
    <tbody>
        @if($bzrloc2->count())
            @php
            $counting = 0
            @endphp
            @foreach($bzrloc2 as $bzr)
            @php
            $counting++
            @endphp
            <tr>
                <th scope="row">{{$counting}}</th>
                <td>{{$bzr->name}}</td>
                <td>{{$bzr->totalPlot}}</td>
                <td>{{$bzr->plotAvail}}</td>
                <td><a href="{{ route('bazaarLocation.edit', $bzr->bzrloc_id) }}"><button type="submit" class="btn btn-success btn-sm">Edit</button></a></td>
                <td>
                    <form method="POST" action="{{ route('bazaarLocation.destroy', $bzr->bzrloc_id) }}">
                        @csrf
                        @method('DELETE')
                        <button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('Adakah anda pasti ingin membatalkan lokasi tersebut?')">Batal</button>
                    </form>
                </td>
            </tr>
            @endforeach
        @else
        <tr>
            <td colspan="2">
                No record found
            </td>
        </tr>
        @endif
    </tbody>
</table>

【讨论】:

    【解决方案2】:

    当你使用-&gt;get()检索时,你应该使用count()函数来检查Collection是否为空。

    不要使用空,尝试使用如下计数:

    @if($bzrloc2->count() < 1)
         <tr>
           <td colspan="2">
             No record found
           </td>
         </tr>
         @endif
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多