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>
    

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

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮