我正在尝试从 Controller 获取模型的关系,以便能够将关系显示为不是 id 而是该 id 的名称或类型或任何可能的类型。在这种情况下,我试图获取与问题相关的信息,它具有什么响应类型(文本、多个、排名、是或否)以及属于哪个部分(名称)
到目前为止,这是我的 Controller 代码
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,是或否。如果这很重要,该字段是表结构中的 bool 值。
如果我这样做
{{ 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();
关于laravel - 如何将某个模型的关系用 Inertia::render 显示在表格中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58239681/