【问题标题】:How to get Foreign key table data based on the status if status is different of a single row in foreign key table laravel [duplicate]如果外键表laravel中的单行状态不同,如何根据状态获取外键表数据[重复]
【发布时间】:2020-03-13 14:17:49
【问题描述】:

我有 2 个具有外键关系的表。情况是我有一个案例,一个案例有很多修订,每个修订都有自己的状态。现在我想要的是你们可以看到case_id1 有三个不同状态的记录现在我需要如果子表的单个状态针对case_id 1 不同我不想要那个父母和孩子,除非我需要所有数据

PrimaryCaseNo模特

class PrimaryCaseNo extends Model
{
    protected $table = 'primary_case_no_migration';
    protected $fillable = ['case_number', 'patient', 'age', 'primary_medical_case_id'];

    public function primaryMedicalCases()
    {
        return $this->hasMany(MedicalPrimaryCases::class, 'primary_medical_case_id');
    }

    public function user(){

        return $this->belongsTo(User::class, 'user_id');
    }
}

MedicalPrimaryCases模特

class MedicalPrimaryCases extends Model
{
    protected $table = 'medical_primary_cases';

    protected $fillable = ['id', 'receive_date', 'impression_particulars', 'nature_of_patient',
        'case_type_particulars', 'priority', 'airway_bill_number', 'description', 'RX_form', 'x_rays_opg', 'x_rays_ceph',
        'file_assessment', 'stl_1', 'stl_2', 'created_by_reference_table', 'deleted_by', 'deleted_by_reference_table',
        'deleted', 'forwarded', 'processed', 'case_id', 'gender', 'doctor', 'impression_type', 'arch_upper', 'arch_lower', 'radio_graphs_opg',
        'radio_graphs_ceph', 'bite', 'impression_workable_upper', 'impression_workable_lower', 'country', 'city',
        'distributor', 'case_type', 'is_deletable', 'is_deleted', 'forwarded_production', 'processed_production', 'processed_by', 'primary_medical_case_id', 'revisions', 'images_at_modification_stage'];

    public function primaryCaseNo()
    {
        return $this->belongsTo(PrimaryCaseNo::class, 'primary_medical_case_id');
    }

}

控制器方法

public function caseTreatmentSetup(){
  $case_id = array();
        $treatment_setup_cases = MedicalPrimaryCases::select('primary_medical_case_id')->where('status', 'treatment-setup')->get();


        foreach ($treatment_setup_cases as $treatment_setup_case) {
            $case_id[] = $treatment_setup_case->primary_medical_case_id;
        }

        $data['treatment_setup_cases'] = PrimaryCaseNo::with('primaryMedicalCases')->whereIn('id', $case_id)->get();

return view('company.treatment_setup', $data);
}

medical_primary_cases

id      case_id    revison         status
1         1           0           assesemnt
2         1           1           assesment
3         1           2           treatment
4         2           2           assesment
5         3           1           assesmen

primary_case_no_migration

id      case_no    patient_name  age
1       12564        abc         78
2       1256         lkj         63
3       125          bdhf        23

我想要的结果因为 case_id 拥有 2 种不同的状态,比如治疗,如果单行状态不同,我只想要评估,我不想要那个。

id      case_id    revison         status
4         2           2           assesment
5         3           1           assesment

【问题讨论】:

  • 请不要重复问同一个问题。如果对原始问题提供的答案没有回答,请编辑您的问题以提供更多信息。

标签: php mysql laravel


【解决方案1】:
  1. 你在关系方法中设置了错误的键
class PrimaryCaseNo extends Model
{
    public function primaryMedicalCases()
    {
        return $this->hasMany(MedicalPrimaryCases::class, 'case_id');
    }
}
class MedicalPrimaryCases extends Model
{
    public function primaryCaseNo()
    {
        return $this->belongsTo(PrimaryCaseNo::class);
    }
}

你需要使用雄辩的关系。解释

public function caseTreatmentSetup(PrimaryCaseNo $model){

$data['treatment_setup_cases'] = model->primaryMedicalCases()->where('status', 'treatment-setup')->get();

return view('company.treatment_setup', $data );
}

【讨论】:

  • 工作它返回空数组()
猜你喜欢
  • 2020-06-23
  • 2015-11-09
  • 2016-10-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-06-13
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多