Laravel - 使用@foreach

时间:2016-09-25 04:54:04

标签: php laravel foreach

我试图从我的数据库中以表格格式显示数据。 thead显示在表格中,但tbody赢了。我认为,问题出在@foreach函数中。因为,我试图在@foreach函数之外键入内容,然后显示出来。而且,我试图移动@foreach <tr>,但结果是一样的。这是我的代码的一部分:

<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>

希望你能提供帮助。感谢。

1 个答案:

答案 0 :(得分:2)

试试这个,如果您使用的是刀片文件,则可以使用{{}}代替;

首先,您可以检查$ student是否有数据。 在返回视图之前的控制器中,您可以执行

return $student;

刷新页面。如果数据正确,请在您的视图中使用此代码。

<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>