drfals1307 2016-11-10 23:28
浏览 65

刀片模板中的foreach无法正常工作

I'm having issues on displaying my data from foreach loop. I have 400+ thumbs on my database but laravel doesn't not work correctly, and my footer template didn't display too. I will put my code below.

@foreach($thumbs as $thumb)
   {{$thumb['name']}}
   {{$thumb['desc']}}
   {{$thumb['place']}}
@endforeach

myfooter code goes here.

from my controllers

$data['thumbs'] = thumb::all();
return View('tubetour/home',$data);

but when I tried to var_dump or return the value of thumbs on my controller it displays all my 400+ data.

$data['thumbs'] = thumb::all();
return $data['thumbs'];
  • 写回答

4条回答 默认 最新

  • doutongfu9484 2016-11-10 23:37
    关注

    If Thumb is an Eloquent model, then the Thumb::all() will return an Eloquent collection, not an array. In that case you have to update your blade template like so:

    @foreach($thumbs as $thumb)
        {{ $thumb->name }}
        {{ $thumb->desc }}
        {{ $thumb->place }}
    @endforeach
    

    Hope this solve your issue.

    UPDATE

    Pass the $thumbs as an array and display it as table.

    Update your controller like this:

    $data['thumbs'] = thumb::all()->toArray();
    return View('tubetour/home', $data);
    

    And your view like this, see how many rows being displayed.

    <table>
        <thead>
            <tr>Id</tr>
            <tr>Name</tr>
            <td>Desc</td>
            <td>Place</td>
        </thead>
        <tbody>
            @for ($i = 0; $i < count($thumbs); $i++)
                <tr>
                    <td>{{ $i }}</td>
                    <td>{{ $thumbs[$i]['name'] }}</td>
                    <td>{{ $thumbs[$i]['desc'] }}</td>
                    <td>{{ $thumbs[$i]['place'] }}</td>
                </tr>
            @endfor
        </tbody>
    </table>
    

    UPDATE 2

    Blade template example with bootstrap grid:

    <div class="row">
        @for ($i = 0; $i < count($thumbs); $i++)
            <div class="col-md-4">
                <img src="img/sample.jpg">
                <h3>{{ $thumbs[$i]['name'] }}</h3>
                {{ $thumbs[$i]['desc'] }}
            </div>
        @endfor
    </div>
    
    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向