【问题标题】:Laravel - Show Data From Database With @foreachLaravel - 使用 @foreach 显示数据库中的数据
【发布时间】:2016-09-25 04:54:04
【问题描述】:

我正在尝试以表格格式显示我的数据库中的数据。 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>

希望您能提供帮助。谢谢。

【问题讨论】:

  • 你会得到什么样的错误?
  • @ChoncholMahmud 我没有收到任何错误,只是数据库中的数据不会显示
  • @ChoncholMahmud 这是代码:public function index() { $student = Student::all(); return view('student.index', compact('student')); } var_dump 是什么?我应该把它放在哪里?
  • 在 foreach 循环 &lt;?php dd($student); ?&gt;&lt;?php var_dump($student); ?&gt; 之前添加任何一个,然后让我知道输出。
  • 注释掉返回视图的行。现在在这之后添加这一行return $student;,看看你是否得到任何 json 响应作为回报。

标签: php laravel foreach


【解决方案1】:

试试这个,如果你使用刀片文件,你可以使用 {{ }} 代替 ;

首先你可以检查 $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>

【讨论】:

  • 当我添加return $student 时,它会显示空数组括号。但是,我的数据库中已经有 10 个数据
  • 我发现了错误。谢谢
猜你喜欢
  • 2021-08-08
  • 1970-01-01
  • 2023-03-18
  • 1970-01-01
  • 2015-10-12
  • 2019-07-07
  • 2021-09-19
  • 2018-02-01
  • 2015-04-18
相关资源
最近更新 更多