【发布时间】:2021-04-14 01:44:21
【问题描述】:
如何从数据库中循环数据我在 laravel 中的路线是 Route::get('api/contacts', [ContactController::class, 'index'])->name('contact.index');我试图显示所有列表,但我与这里专家的 js 代码混淆,请帮助我
类 ContactController 扩展控制器 { /** * 显示资源列表。 * * @return \Illuminate\Http\Response */ 公共函数索引() { $contacts = Contact::all();
// return view('contacts.index', compact('contacts'));
return response()->json(compact('contacts'));
}
<script>
import axios from "axios";
export default {
data() {
return {
form: {
first_name: "",
last_name: "",
email: "",
city: "",
country: "",
job_title: "",
},
errorMessage: "",
user: "",
};
},
methods: {
processCreate() {
this.errorMessage = "";
axios
.post("/api/contacts/index")
.then((response) => {})
.catch((error) => {
this.errorMessage = error.response.data.message;
console.log("error", error.response);
});
console.log(response);
},
},
mounted() {
// console.log(this.form)
},
};
</script>
<template>
<div class="row">
<div class="col-sm-12">
<h1 class="display-3">Contacts</h1>
<table class="table table-striped">
<thead>
<tr>
<td>ID</td>
<td>Name</td>
<td>Email</td>
<td>Job Title</td>
<td>City</td>
<td>Country</td>
<td colspan="3">Actions</td>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td></td>
<td>
<a href="" class="btn btn-warning">Show </a>
</td>
<td>
<a href="" class="btn btn-primary">Edit</a>
</td>
<td>
<form method="post" action="">
<button class="btn btn-danger" type="submit">Delete</button>
</form>
</td>
</tr>
</tbody>
</table>
<div>
<router-link :to="{ name: 'contactsCreate' }">New Contact</router-link>
</div>
</div>
</div>
</template>
【问题讨论】:
-
你试过我的解决方案了吗?