当应答者表与年龄表、性别表、疾病表和疫苗表存在一对一关系时,如何从应答者表中检索数据?
我想在我的table.blade.php文件中显示我的respondent表中的内容,但是当我试图显示年龄、性别、疾病和疫苗时,它只显示了它的ID,而不是它们各自表中的内容。
响应者模式
public function up(): void
{
Schema::create('respondents', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->unsignedInteger('id_purok');
$table->unsignedInteger('id_household');
$table->unsignedInteger('id_age');
$table->unsignedInteger('id_sex');
$table->unsignedInteger('id_marital_status');
$table->unsignedInteger('id_means_of_living');
$table->unsignedInteger('id_educational_attainment');
$table->unsignedInteger('id_physical_condition');
$table->unsignedInteger('id_disease');
$table->unsignedInteger('id_vacinne');
$table->timestamps();
$table->foreign('id_purok')->references('id')->on('puroks');
$table->foreign('id_household')->references('id')->on('household_heads');
$table->foreign('id_age')->references('id')->on('ages');
$table->foreign('id_sex')->references('id')->on('sexes');
$table->foreign('id_marital_status')->references('id')->on('marital_statuses');
$table->foreign('id_means_of_living')->references('id')->on('means_of_livings');
$table->foreign('id_educational_attainment')->references('id')->on('educational_attainments');$table->foreign('id_physical_condition')->references('id')->on('physical_conditions');
$table->foreign('id_disease')->references('id')->on('diseases');
$table->foreign('id_vacinne')->references('id')->on('vaccines');
});
}
字符串
年龄表
public function up(): void
{
Schema::create('ages', function (Blueprint $table) {
$table->increments('id');
$table->string('age');
$table->timestamps();
});
}
型
性别表
public function up(): void
{
Schema::create('sexes', function (Blueprint $table) {
$table->increments('id');
$table->string('sex');
$table->timestamps();
});
}
型
疾病表
public function up(): void
{
Schema::create('diseases', function (Blueprint $table) {
$table->increments('id');
$table->string('disease');
$table->timestamps();
});
}
型
疫苗表
public function up(): void
{
Schema::create('vaccines', function (Blueprint $table) {
$table->increments('id');
$table->string('vaccine');
$table->timestamps();
});
}
型
答辩人控制人
public function index()
{
$respondents = Respondent::all();
$households = HouseholdHead::all();
$puroks = Purok::all();
$diseases = Disease::all();
$vaccines = Vaccine::all();
return view('respondent_table', compact('respondents', 'households', 'puroks', 'diseases', 'vaccines'));
}
型
受访者模型
class Respondent extends Model
{
use HasFactory;
public function vaccine()
{
return $this->belongsTo(Vaccine::class);
}
public function disease()
{
return $this->belongsTo(Disease::class);
}
public function age()
{
return $this->belongsTo(Age::class);
}
public function sex()
{
return $this->belongsTo(Sex::class);
}
}
型
table.blade.php
<table class="table table-striped " id="dataTable">
</div>
<thead>
<tr>
<th>Surename</th>
<th>Purok</th>
<th>Age</th>
<th>Sex</th>
<th>Disease</th>
<th>Vaccine</th>
<th>Physical Condition</th>
<th>Date</th>
</tr>
</thead>
<tbody>
@foreach ($respondents as $respondent)
<tr>
<td> {{$respondent->name}} </td>
<td> {{$respondent->id_purok}} </td>
<td> {{$respondent->id_age}} </td>
<td> {{$respondent->id_sex}} </td>
<td> {{$respondent->id_disease}} </td>
<td> {{$respondent->id_vacinne}} </td>
<td> {{$respondent->id_physical_condition}} </td>
<td> {{$respondent->created_at}} </td>
</tr>
@endforeach
</tbody>
</table>
型
2条答案
按热度按时间ac1kyiln1#
您没有从关系中提取相关数据。
在查询中,您将执行以下操作,
字符串
然后您将可以访问下面的相关数据,
型
你就可以像这样
型
hlswsv352#
首先更新您的控制器。
字符串
您可以修改
table.blade.php
文件以显示相关表中的内容。型
此外,确保在响应者模型中正确定义了关系
型