【问题标题】:Object of class Illuminate\Database\Eloquent\Collection could not be converted to int Laravel 5.4类 Illuminate\Database\Eloquent\Collection 的对象无法转换为 int Laravel 5.4
【发布时间】:2019-06-27 16:21:30
【问题描述】:

我有一个充满信息的旧数据库,现在我想显示该数据库中的类别名称并收到此错误。

这是我的控制器

public function forums(){

    $cats = Forum_cats::all();
    return view ('lapas.forums.index')->with('cats', $cats);
    }
}

这是我的看法

@if(count($cats >1))
    @foreach($cats as $cati)
        <div class = "well">
            <h3>{{$cati->description}}</hr>
        </div>
    @endforeach

@else

@endif

这是DB结构的屏幕

http://prntscr.com/mg5nk1

如果需要,请询问更多信息!

【问题讨论】:

  • 尝试将@if(count($cats &gt;1)) 替换为@if($cats-&gt;count())。再加上你的 &gt; 1 是错误的父母

标签: php laravel


【解决方案1】:

您的if 错误。试试看:

@if($cats->count() > 1)

【讨论】:

    【解决方案2】:

    您的操作员似乎放错了位置:

    @if(count($cats) > 1)
        @foreach($cats as $cati)
            <div class = "well">
                <h3>{{$cati->description}}</hr>
            </div>
        @endforeach
    
    @else
    
    @endif
    

    您似乎正试图在 Blade 模板中循环遍历这些 $cats。你也可以试试forelse

    @forelse($cats as $cati)
      <div class = "well">
        <h3>{{$cati->description}}</hr>
      </div>
    @empty
      {{-- Action if there are none --}}
    @endforelse
    

    编辑:此处的文档:https://laravel.com/docs/5.4/blade#loops

    【讨论】:

      猜你喜欢
      • 2015-09-23
      • 2019-01-10
      • 2019-05-29
      • 2020-05-18
      • 1970-01-01
      • 2018-10-22
      • 2021-11-08
      • 2020-12-01
      • 1970-01-01
      相关资源
      最近更新 更多