在 Laravel Blade 中显示表中的相关数据

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

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

我想在我的 table.blade.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>
php laravel eloquent laravel-blade
2个回答
0
投票

您没有从关系中提取相关数据。

在查询中你将执行类似的操作,

$respondents = Respondent::with('vaccine', 'disease', 'age', 'otherRelationsIfAny')->get();

然后您将可以访问如下相关数据,

@foreach ($respondents as $respondent)
{{ $respondent->vaccine->vaccine }}
{{ $respondent->disease->disease}}
@endforeach

您将像这样访问

$respondent->Relationship->RelationshipTableColumns

0
投票

首先更新您的控制器。

public function index()
{
    $respondents = Respondent::with('age', 'sex', 'disease', 'vaccine')->get();
    $households = HouseholdHead::all();
    $puroks = Purok::all();
    $diseases = Disease::all();
    $vaccines = Vaccine::all();

    return view('respondent_table', compact('respondents', 'households', 'puroks', 'diseases', 'vaccines'));
}

您可以修改您的

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.