作为程序员一定要保持良好的睡眠,才能好编程

laravel关联模型之一对一、一对多(多态)

发布时间:2020-02-20



laravel官方文档:

https://learnku.com/docs/laravel/6.x/eloquent-relationships/5177#6c38b1



详细说明:


在一张表中根据类型找到关联id,寻找到自己需要的资源。



image.png


目前项目中没有使用到,暂不做介绍,当使用到以后,再做补充。


<?php

namespace App\Models\Inquiry;

use App\Core\Model;


class OutPatientOrderModel extends Model
{
    protected $table = 'outpatient_order';

    protected $primaryKey = 'id';

    protected $keyType = 'string';

    protected $connection = 'ehosp';

    protected $hidden = [
        'pkid'
    ];

    const CREATED_AT = 'gmt_created';

    const UPDATED_AT = 'gmt_modified';

    protected $guarded = [];

    protected $casts = [
        'register_order_id' => 'string',
    ];


    public function detail()
    {
        return $this->belongsTo(OnlineOrderDetail::class, 'register_order_id', 'order_id')->withDefault();
    }

    
    public function pay()
    {
        return $this->hasOne(PayOrderModel::class, 'order_id', 'register_order_id')
            ->where('business_type', 3)->withDefault();
    }

     
    public function medical()
    {
        return $this->belongsTo(MedicalRecordModel::class, 'medical_record_id',
            'id')->withDefault();
    }

   
    public function initialMedical()
    {
        return $this->hasMany(MedicalInitialDiagModel::class, 'medical_record_id', 'medical_record_id');
        /*return $this->belongsTo(MedicalInitialDiagModel::class, 'medical_record_id',
            'medical_record_id')->withDefault();*/
    }


     
    public function clinicDoctor()
    {
        return $this->belongsTo(DoctorModel::class, 'clinic_doctor_id', 'weiyi_expertid')->withDefault();
    }


     
    public function applyDoctor()
    {
        return $this->belongsTo(DoctorModel::class, 'apply_doctor_id', 'weiyi_expertid')->withDefault();
    }

 
    public function refund()
    {
        return $this->hasOne(PayRefundModel::class, 'order_id', 'id')
            ->where('business_type', 3)->withDefault();
    }

    
    public function prescription()
    {
        return $this->hasManyThrough(PrescriptionOrderModel::class, OutpatientPrescriptionRelationModel::class,
            'outpatient_order_id', 'id', 'id', 'prescription_order_id');
    }

  
    public function clinicDoctorLevel()
    {
        return $this->hasOneThrough(DoctorLevelModel::class, DoctorModel::class,
            'weiyi_expertid',
            'doclevelid',
            'clinic_doctor_id',
            'doctor_level'
        )->withDefault();
    }

    
    public function drugs()
    {
        return $this->hasMany(OnlineDrugsDetail::class, 'order_id', 'register_order_id');
    }

    
    public function disease()
    {
        return $this->hasMany(OnlineDiseaseDetail::class, 'order_id', 'register_order_id');
    }
}