【发布时间】:2021-05-14 21:44:28
【问题描述】:
我在 laravel 和 vue 关于显示数据库表查找方法的结果时遇到问题。我不太明白的是为什么 v-for 指令解析 json 结果不正确。
这是 Vue 代码:
<template>
<table class="table table-hover">
<thead>
<tr>
<th>Class</th>
<th>Amount of Students</th>
<th>Teacher</th>
<th>Action</th>
</tr>
</thead>
<tbody>
<tr v-for="classroom in classrooms" :key="classroom.id">
<td>{{ classroom.class_no }}•{{ classroom.field }}•{{ classroom.room_no }}</td>
<td>{{ classroom.amount_students }}</td>
<td>{{ classroom.teacher }}</td>
<td>
<a href="#">
<i class="fa fa-edit blue"></i>
</a>
</td>
</tr>
</tbody>
</table>
</template>
<script>
export default {
data () {
return {
classrooms : {
"success": true,
"data": { "id": 1, "class_no": 1, "field": "Architecture", "room_no": 4, "amount_students": 40, "teacher": "Bobby Fisher" },
"message": "Find Classroom Detail"
}
}
}
}
</script>
json 教室本身实际上是控制器的结果:
public function show($level)
{
$classrooms = ClassRoom::where('class_no', $level)->firstOrFail();
return $this->sendResponse($classrooms , 'Find Classroom Detail');
}
这是错误结果的截图:
The result should be only a single row
请帮我解决这个问题。
【问题讨论】:
-
欢迎来到 SO .. 这是您的完整组件代码吗?
-
不,但这段代码只是我完整代码的一部分。这个问题是我会员 4 年后的第二个问题。大声笑
标签: json laravel vue.js controller v-for