How can I retrieve data from my respondent table when it has a one-to-one relationship with the ages table, sexes table, diseases table, and vaccines table?
I want to display within my table.blade.php file the contents in my respondent table, but when I tried to display the age, sex, disease, and vaccine, it only displayed its ID and not its content from their individual tables.
Respondent schema
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');
});
}
Ages table
public function up(): void
{
Schema::create('ages', function (Blueprint $table) {
$table->increments('id');
$table->string('age');
$table->timestamps();
});
}
Sexes table
public function up(): void
{
Schema::create('sexes', function (Blueprint $table) {
$table->increments('id');
$table->string('sex');
$table->timestamps();
});
}
Diseases table
public function up(): void
{
Schema::create('diseases', function (Blueprint $table) {
$table->increments('id');
$table->string('disease');
$table->timestamps();
});
}
Vaccines table
public function up(): void
{
Schema::create('vaccines', function (Blueprint $table) {
$table->increments('id');
$table->string('vaccine');
$table->timestamps();
});
}
Respondent controller
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'));
}
Respondent model
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
Answers
First update your controller.
You can modify your
table.blade.php
file to display the content from related tables.you did not pulled the related data from the relationships.
in the query you will do something like this,
and then you will have access to the related data like below,
you will access just like this