【发布时间】: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 循环
<?php dd($student); ?>或<?php var_dump($student); ?>之前添加任何一个,然后让我知道输出。 -
注释掉返回视图的行。现在在这之后添加这一行
return $student;,看看你是否得到任何 json 响应作为回报。