laravel 显示相关嵌套数据

2uluyalo  于 2023-11-20  发布在  其他
关注(0)|答案(1)|浏览(95)

我想在我的table.blade.php中显示相关和嵌套的数据,但我只能显示它的id。

**:

x1c 0d1x的数据

sdoh表:


sdoh架构:

public function up(): void
{
    Schema::create('s_d_o_h_s', function (Blueprint $table) {
        $table->increments('id');
        $table->string('sdoh');
        $table->timestamps();
    });
}

字符串

SDOH模型

类SDOH扩展Model{ use HasFactory;

public function sdoh_category(){
    return $this->hasOne(SDOHCategory::class, 'sdoh_id', 'id');
}

public function household_head(){
    return $this->hasOne(HouseholdHead::class, 'sdoh_id', 'id');
}


}

sdoh_category表:


sdoh_category架构:

public function up(): void
{
    Schema::create('s_d_o_h_categories', function (Blueprint $table) {
        $table->increments('id');
        $table->string('category');
        $table->unsignedInteger('sdoh_id');
        $table->timestamps();

        $table->foreign('sdoh_id')->references('id')->on('s_d_o_h_s');
    });
}

SDOH类别模型

class SDOHCategory extends Model


{ use HasFactory;

public function sdoh(){
    return $this->belongsTo(SDOH::class, 'sdoh_id');
}

public function residence(){
    return $this->hasOne(HouseholdHead::class, 'residence');
}

public function house_construction(){
    return $this->hasOne(HouseholdHead::class, 'house_construction');
}

public function water_source(){
    return $this->hasOne(HouseholdHead::class, 'water_source');
}

public function toilet_facility(){
    return $this->hasOne(HouseholdHead::class, 'toilet_facility');
}

public function electricity_source(){
    return $this->hasOne(HouseholdHead::class, 'electricity_source');
}

public function combined_family_income(){
    return $this->hasOne(HouseholdHead::class, 'combined_family_income');
}

public function religion(){
    return $this->hasOne(HouseholdHead::class, 'religion');
}

public function family_type(){
    return $this->hasOne(HouseholdHead::class, 'family_type');
}

public function garbage_disposal(){
    return $this->hasOne(HouseholdHead::class, 'garbage_disposal');
}

public function dental_healthcare(){
    return $this->hasOne(HouseholdHead::class, 'dental_healthcare');
}

public function preventive_procedure(){
    return $this->hasOne(HouseholdHead::class, 'preventive_procedure');
}

public function medical_services(){
    return $this->hasOne(HouseholdHead::class, 'medical_services');
}

public function healthcare(){
    return $this->hasOne(HouseholdHead::class, 'healthcare');
}

public function exposure_to_unhealthy_behaviour(){
    return $this->hasOne(HouseholdHead::class, 'exposure_to_unhealthy_behaviour');
}

public function health_insurance(){
    return $this->hasOne(HouseholdHead::class, 'health_insurance');
}

public function env_hazards(){
    return $this->hasOne(HouseholdHead::class, 'env_hazards');
}


}
sdoh_category表和sdoh有一对一的关系。我有一个household_head表,我希望用户从描述他们家庭的类别中选择。household_head表:


household_head schema:

public function up(): void
{
    Schema::create('household_heads', function (Blueprint $table) {
        $table->increments('id');
        $table->string('name');
        $table->unsignedInteger('purok_id');
        $table->unsignedInteger('residence');
        $table->unsignedInteger('house_construction');
        $table->unsignedInteger('water_source');
        $table->unsignedInteger('toilet_facility');
        $table->unsignedInteger('electricity_source');
        $table->unsignedInteger('combined_family_income');
        $table->unsignedInteger('religion');
        $table->unsignedInteger('family_type');
        $table->unsignedInteger('garbage_disposal');
        $table->unsignedInteger('dental_healthcare');
        $table->unsignedInteger('preventive_procedure');
        $table->unsignedInteger('medical_services');
        $table->unsignedInteger('healthcare');
        $table->unsignedInteger('exposure_to_unhealthy_behaviour');
        $table->unsignedInteger('health_insurance');
        $table->unsignedInteger('env_hazards');
        $table->integer('num_of_adults');
        $table->timestamps();

        $table->foreign('purok_id')->references('id')->on('puroks');
        $table->foreign('residence')->references('id')->on('s_d_o_h_categories');
        $table->foreign('house_construction')->references('id')->on('s_d_o_h_categories');
        $table->foreign('water_source')->references('id')->on('s_d_o_h_categories');
        $table->foreign('toilet_facility')->references('id')->on('s_d_o_h_categories');
        $table->foreign('electricity_source')->references('id')->on('s_d_o_h_categories');
        $table->foreign('combined_family_income')->references('id')->on('s_d_o_h_categories');
        $table->foreign('religion')->references('id')->on('s_d_o_h_categories');
        $table->foreign('family_type')->references('id')->on('s_d_o_h_categories');
        $table->foreign('garbage_disposal')->references('id')->on('s_d_o_h_categories');
        $table->foreign('dental_healthcare')->references('id')->on('s_d_o_h_categories');
        $table->foreign('preventive_procedure')->references('id')->on('s_d_o_h_categories');
        $table->foreign('medical_services')->references('id')->on('s_d_o_h_categories');
        $table->foreign('healthcare')->references('id')->on('s_d_o_h_categories');
        $table->foreign('exposure_to_unhealthy_behaviour')->references('id')->on('s_d_o_h_categories');
        $table->foreign('health_insurance')->references('id')->on('s_d_o_h_categories');
        $table->foreign('env_hazards')->references('id')->on('env_hazards');
    });
}

HouseholdHead型号:

<?php


命名空间App\Models;
使用Illuminate\Database\Eloquent\Factories\HasFactory;使用Illuminate\Database\Eloquent\Model;
类HouseholdHead扩展Model { use HasFactory;

public function purok(){
    return $this->belongsTo(Purok::class, 'purok_id');
}

public function respondent(){
    return $this->hasOne(Respondent::class, 'household_head_id', 'id');
}

public function sdoh(){
    return $this->hasOne(SDOH::class, 'household_id', 'id');
}

public function residence(){
    return $this->belongsTo(SDOHCategory::class, 'residence');
}

public function house_construction(){
    return $this->belongsTo(SDOHCategory::class, 'house_construction');
}

public function water_source(){
    return $this->belongsTo(SDOHCategory::class, 'water_source');
}
public function toilet_facility(){
    return $this->belongsTo(SDOHCategory::class, 'toilet_facility');
}

public function electricity_source(){
    return $this->belongsTo(SDOHCategory::class, 'electricity_source');
}

public function combined_family_income(){
    return $this->belongsTo(SDOHCategory::class, 'combined_family_income');
}

public function religion(){
    return $this->belongsTo(SDOHCategory::class, 'religion');
}

public function family_type(){
    return $this->belongsTo(SDOHCategory::class, 'family_type');
}

public function garbage_disposal(){
    return $this->belongsTo(SDOHCategory::class, 'garbage_disposal');
}

public function dental_healthcare(){
    return $this->belongsTo(SDOHCategory::class, 'dental_healthcare');
}

public function preventive_procedure(){
    return $this->belongsTo(SDOHCategory::class, 'preventive_procedure');
}
public function medical_services(){
    return $this->belongsTo(SDOHCategory::class, 'medical_services');
}

public function healthcare(){
    return $this->belongsTo(SDOHCategory::class, 'healthcare');
}

public function exposure_to_unhealthy_behaviour(){
    return $this->belongsTo(SDOHCategory::class, 'exposure_to_unhealthy_behaviour');
}

public function health_insurance(){
    return $this->belongsTo(SDOHCategory::class, 'health_insurance');
}

public function env_hazards(){
    return $this->belongsTo(SDOHCategory::class, 'env_hazards');
}


}

table.blade.php code:

@foreach ($households as $household)
       <tr  >
            <td class="pr-3">{{$household->id}}</td>
            <td class="pr-3"> {{$household->name}} </td>
            <td class="pr-3"> {{$household->purok->purok_num}} </td>
            <td class="pr-3"> {{$household->residence}} </td>
            <td class="pr-3"> {{$household->house_construction}} </td>
             <td class="pr-3"> {{$household->water_source}} </td>
            <td class="pr-3"> {{$household->toilet_facility}} </td>
             <td class="pr-3"> {{$household->electricity_source}} </td>
             <td class="pr-3"> {{$household->combined_family_income}} </td>                                                                     
              <td class="pr-3"> {{$household->religion}} </td>
            <td class="pr-3"> {{$household->family_type}} </td>
            <td class="pr-3"> {{$household->garbage_disposal}} </td>
            <td class="pr-3"> {{$household->dental_healthcare}} </td>
            <td class="pr-3"> {{$household->preventive_procedure}} </td>
            <td class="pr-3"> {{$household->medical_services}} </td>
            <td class="pr-3"> {{$household->healthcare}} </td>
                                                                      
            <td class="pr-3"> {{$household->exposure_to_unhealthy_behaviour}} </td>
            <td class="pr-3"> {{$household->health_insurance}} </td>
            <td class="pr-3"> {{$household->env_hazards}} </td>
            <td class="pr-3"> {{$household->num_of_adults}} </td>
            <td class="pr-3"> {{$household->created_at}} </td>

      </tr>
 @endforeach

控制器:

public function index()
{
$ages = Age::all();
$SDOHs = SDOH::all();
$categories = SDOHCategory::all();
$sexes = Sex::all();
$diseases = Disease::all();
$vaccines = Vaccine::all();
$envHazards = EnvHazard::all();
$respondents = Respondent::with('household_heads')->get();
$households = HouseholdHead::with('residence','house_construction', 
'water_source', 'toilet_facility', 'electricity_source', 
'combined_family_income', 'religion', 'family_type', 
'garbage_disposal', 'dental_healthcare', 'preventive_procedure', 
'medical_services', 'healthcare', 'exposure_to_unhealthy_behaviour', 
'health_insurance', 'env_hazards' )->get();

return view('map', compact('households',  'SDOHs', 'categories',   
'diseases', 'vaccines', 'envHazards', 'respondents'));


}
我想在table.blade.php中显示用户选择的类别,但我只能显示类别的ID。

cnh2zyt3

cnh2zyt31#

您只需要更改数据库结构,然后Db schema
然后

$categoryId = 1;

$heads = DB::table('household_head', 'hh')
  ->selectRaw('hh.*')
  ->join('sdoh_category_heads as h', 
     fn($join) => $join->on('hh.id', 'h.head_id')->where('h.category_id', $categoryId)
  )
  ->get();

字符串

相关问题