dtstnjl898781429 2015-10-08 02:12
浏览 58

Laravel5.1查询关系(Eloquent ORM)

My model is Patient->Sample->Ready_Sample ,relationship all is oneToMany , My question is I query Ready_Sample need to know patient.name

Patient_Model

class Patient_Model extends Base_Model {

    protected $table = 'patients';

    public function samples(){
        return $this->hasMany('App\Models\Sample_Model','patient_id','code');
    }   
}

Sample_Model

class Sample_Model extends Base_Model{

    protected $table = 'samples';

    public function patient(){
        return $this->belongsTo('App\Models\Patient_Model','patient_id','code');
    }

    public function ready_samples(){
        return $this->hasMany('App\Models\Ready_Sample_Model','sample_id','code');
    }
}

Ready_Sample_Model

class Ready_Sample_Model extends Model{

    protected $table = 'ready_samples';

    public function sample(){
        return $this->belongsTo('App\Models\Sample_Model','sample_id','code');
    }
}

In Sample_Controller

class Sample_Controller extends Controller{

    public function query(Request $request){

    $result = Sample_Model::with(['patient']);
        ->orderBy("updated_at","desc")
        ->Paginate(15)
        ->toJson();
   return $result;
}

In Sample I know to get patient.name ,but Ready_Sample how to get Patien.name?

  • 写回答

1条回答 默认 最新

  • dongying7667 2015-10-08 03:43
    关注

    You can get the patient.name using the below code:

    $readySample = Ready_Sample_Model::first(); // fetch the first record
    
    echo $readySample->sample->patient->name;
    

    Hope it helps!

    评论

报告相同问题?

悬赏问题

  • ¥15 ogg dd trandata 报错
  • ¥15 高缺失率数据如何选择填充方式
  • ¥50 potsgresql15备份问题
  • ¥15 Mac系统vs code使用phpstudy如何配置debug来调试php
  • ¥15 目前主流的音乐软件,像网易云音乐,QQ音乐他们的前端和后台部分是用的什么技术实现的?求解!
  • ¥60 pb数据库修改与连接
  • ¥15 spss统计中二分类变量和有序变量的相关性分析可以用kendall相关分析吗?
  • ¥15 拟通过pc下指令到安卓系统,如果追求响应速度,尽可能无延迟,是不是用安卓模拟器会优于实体的安卓手机?如果是,可以快多少毫秒?
  • ¥20 神经网络Sequential name=sequential, built=False
  • ¥16 Qphython 用xlrd读取excel报错