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>
    

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

报告相同问题?

悬赏问题

  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容