doutuo7609 2016-09-25 04:54
浏览 248
已采纳

Laravel - 使用@foreach从数据库显示数据

I'm trying to show data in table format from my DB. thead shows up in the table, but the tbody won't show up. I think, the problem is in the @foreach function. Because, I have tried to type something outside the @foreach function, and it shows up. And, I have tried to move the @foreach inside <tr>, but the result is just the same. Here's a part of my code:

<table class="table table-striped table-bordered" border="1px solid black">
    <thead>
        <tr>
            <td>ID</td>
            <td>Name</td>
            <td>Created At</td>
            <td>Updated At</td>
            <td>Date of birth</td>
            <td>Gender</td>
            <td>Action</td>
        </tr>
    </thead>
    <tbody>
        @foreach($student as $key => $value)
        <!--$student is imported from another file,
        with this function: $student = Student::all()-->
        <tr>
            <td>{{$value->id}}</td>
            <td>{{$value->name}}</td>
            <td>{{$value->created_at}}</td>
            <td>{{$value->updated_at}}</td>
            <td>{{$value->dob}}</td>
            <td>{{$value->gender}}</td>
            <td><a href="{{URL::to('student/' . $value->id)
                }}">Show</a> | 
                <a href="{{URL::to('student/' . $value->id)
                    .'/edit'}}">Edit</a> |
                <form method="post" action="<?php echo url('/')
                    ."/student/".$value->id."/delete";?>">
                    <input type="hidden" name="_token" value="
                        {!!csrf_token() !!}">
                    <div class="form-group">
                        <div>
                            <button type="submit" class=
                                "btn btn-warning">Delete</button>
                        </div>
                    </div>
                </form>
            </td>
        </tr>
        @endforeach
    </tbody>
</table>

Hope you can help. Thanks.

  • 写回答

1条回答 默认 最新

  • douzhan3900 2016-09-25 06:13
    关注

    Try this, If you are using blade file you can use {{ }} in place of ;

    First you can check $student has data or not. In your controller just before returning view you can do

    return $student;
    

    Refresh your page. If data is coming correctly then use this code in your view.

    <table class="table table-striped table-bordered" border="1px solid black">
    <thead>
        <tr>
            <td>ID</td>
            <td>Name</td>
            <td>Created At</td>
            <td>Updated At</td>
            <td>Date of birth</td>
            <td>Gender</td>
            <td>Action</td>
        </tr>
    </thead>
    <tbody>
        @foreach($student as $value)
        <tr>
            <td>{{$value->id}}</td>
            <td>{{$value->name}}</td>
            <td>{{$value->created_at}}</td>
            <td>{{$value->updated_at}}</td>
            <td>{{$value->dob}}</td>
            <td>{{$value->gender}}</td>
            <td><a href="student/". $value->id>Show</a> | 
                <a href="student/" . $value->id ."/edit">Edit</a> |
                <form method="post" 
                 action="/student/".$value->id."/delete">
                    <input type="hidden" name="_token" value="
                        {!!csrf_token() !!}">
                    <div class="form-group">
                        <div>
                            <button type="submit" class=
                                "btn btn-warning">Delete</button>
                        </div>
                    </div>
                </form>
            </td>
        </tr>
        @endforeach
    </tbody>
    

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里