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

7fyelxc5  于 12个月前  发布在  其他
关注(0)|答案(2)|浏览(135)

当应答者表与年龄表、性别表、疾病表和疫苗表存在一对一关系时,如何从应答者表中检索数据?
我想在我的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>

ac1kyiln

ac1kyiln1#

您没有从关系中提取相关数据。
在查询中,您将执行以下操作,

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

字符串
然后您将可以访问下面的相关数据,

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


你就可以像这样

$respondent->Relationship->RelationshipTableColumns

hlswsv35

hlswsv352#

首先更新您的控制器。

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>


此外,确保在响应者模型中正确定义了关系

class Respondent extends Model
{
    use HasFactory;

    public function vaccine()
    {
        return $this->belongsTo(Vaccine::class, 'id_vacinne');
    }

    public function disease()
    {
        return $this->belongsTo(Disease::class, 'id_disease');
    }

    public function age()
    {
        return $this->belongsTo(Age::class, 'id_age');
    }

    public function sex()
    {
        return $this->belongsTo(Sex::class, 'id_sex');
    }
}

相关问题