laravel - 如何使用 Inertia::render 将某个模型的关系显示在表格中?
问题描述
我正在尝试从控制器获取模型的关系,以便能够将关系显示为不是 id,而是显示为该 id 的名称或类型或任何可能的内容。在这种情况下,我试图获取与问题有关的信息,它具有什么响应类型(文本、多个、排名、是或否)以及属于哪个部分(名称)
到目前为止,这是我的控制器代码
public function index()
{
return Inertia::render('Question/Index', [
'survey_question' => SurveyQuestion::all(),
'survey_section' => SurveySection::all(),
'response_type' => ResponseType::all()
]);
}
vue中的表格
<el-table
:data="tableData">
<el-table-column
prop="question"
label="Pregunta">
</el-table-column>
<el-table-column
label="Seccion">
<template slot-scope="scope">
<p> {{ scope.row.survey_section.title }} </p>
</template>
</el-table-column>
<el-table-column
label="Tipo de Respuesta">
<template slot-scope="scope">
<p> {{ scope.row.response_type.type }} </p>
</template>
</el-table-column>
<el-table-column
prop="optional"
label="Opcional">
</el-table-column>
<el-table-column>
<template slot-scope="scope">
<div class="btn-link-edit action-button" @click="edit(scope.row)">
<i class="fas fa-pencil-alt"></i>
</div>
<div class="btn-link-delete action-button" @click="delete(scope.row)">
<i class="fas fa-trash"></i>
</div>
</template>
</el-table-column>
</el-table>
这带来了我认为的关系,因为当我创建一个新问题时,我有一个选择选项,它确实显示了名称而不是 id,但是当我尝试在表中显示所述名称时,我只能访问 id。
我也想知道如何显示可选字段而不是 0 或 1,是或否。如果这很重要,则此字段是表结构中的布尔值。
如果我这样做{{ scope.row }}
了,我会得到信息,但只有这样的问题
{ "id": 1, "question": "asdfasdf", "survey_section_id": 1, "response_type_id": 1, "optional": 1 }
我想要的是,当我这样做时,{{ scope.row }}
我还从这些 id 中获取另一个数组,其中包含与该 id 相关的信息,包括来自部分和响应类型的信息。
解决方案
您应该with
在 eloquent 模型中使用方法。例如:
$result = Model::with(['nameOfTheRelationShip'])->get();
推荐阅读
- c - 用于打印字符串的 ASCII(每个字符)的简单代码程序不适用于输入案例
- python - 我可以在不预先创建工作簿对象的情况下创建 Python Pandas ExcelWriter 格式吗?
- google-chrome - 为在 Chrome DevTools 中重定向的网站添加 cookie?
- typescript - Mocha --watch 在第二次运行时失败,并显示“addEventListener 不是函数”
- amazon-web-services - 无法使用 AWS S3 将域定向到静态页面
- firebase - 没有 FirebaseUser 的电子邮件验证
- python-3.x - 已解决 Python3,类和 tkinter 的困难
- r - 如何计算R中xy点的周长?
- angular - 什么时候需要消毒剂BypassSecurityUrl
- python - 当窗口调整大小时,也会在 tkinter GUI 中调整大小