当我的受访者表与年龄表、性别表、疾病表和疫苗表具有一对一关系时,如何从受访者表中检索数据

问题描述 投票:0回答:1

我想在我的 table.bldae.php 中显示我的受访者表中的内容,但是当我尝试显示年龄、性别、疾病和疫苗时,它只显示其 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>
laravel eloquent laravel-blade
1个回答
0
投票

您可以修改您的

table.blade.php
文件以显示相关表中的内容。

<table class="table table-striped" id="dataTable">
    <thead>
        <tr>
            <th>Surname</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->purok->name }}</td>
            <td>{{ $respondent->age->age }}</td>
            <td>{{ $respondent->sex->sex }}</td>
            <td>{{ $respondent->disease->disease }}</td>
            <td>{{ $respondent->vaccine->vaccine }}</td>
            <td>{{ $respondent->physical_condition }}</td>
            <td>{{ $respondent->created_at }}</td>
        </tr>
        @endforeach
    </tbody>
</table>
© www.soinside.com 2019 - 2024. All rights reserved.