简体   繁体   English

如何通过laravel雄辩模型加入三个表Laravel 5

[英]How to join three table by laravel eloquent model Laravel 5

I searched on the internet for my problem but I couldn't find an answer. 我在互联网上搜索了自己的问题,但找不到答案。 I found a tutorial on StackOverflow , but for my example it didn't worked. 我在StackOverflow上找到了一个教程,但是对于我的示例却没有用。

When I run the following code, it returns NULL : 当我运行以下代码时,它返回NULL

$data['lessons']->student

What i want to get: 我想得到什么:

ID | ID | Date | 日期| User.firstname | User.firstname | Teacher.firstname Teacher.firstname

My code: 我的代码:

I have 3 Tables: 我有3张桌子:

  • lesson {id, date, fk_students_id, fk_teachers_id} 课程{id,date,fk_students_id,fk_teachers_id}
  • student {id, firstname} 学生{id,名}
  • teacher{id, firstname} 老师{id,名}

Model Lesson.php : 模型Lesson.php

public function student(){

    return $this->belongsTo('App\Student');
}

public  function teacher(){

    return $this->belongsTo('App\Teacher');
}

Model Student.php : 模型Student.php

public function lessons(){
    return $this->hasMany('App\Lesson', 'fk_students_id');
}

Model Teacher.php : 模型Teacher.php

public function lessons(){
    return $this->hasMany('App\Lesson', 'fk_teachers_id');
}

And in the Controller : 控制器中

$data['lessons'] = Lesson::with(['student', 'teacher'])->first();
return var_dump($data['lessons']->student);

UPDATE Output of dd($data); UPDATE 输出 dd($data); :

array:1 [
"lessons" => Lesson {#303 
#connection: null
#table: null
#primaryKey: "id"
#perPage: 15
+incrementing: true
+timestamps: true
#attributes: array:10 [
  "id" => 1
  "date" => "2015-07-25"
  "start" => "12:30:00"
  "end" => "13:00:00"
  "fk_students_id" => 1
  "fk_teachers_id" => 1
  "lesson_array" => "[{"id":"1", "result":"0"},{"id":"2", "result":"1"},{"id":"15", "result":"2"}]"
  "slug" => "562153eae355e"
  "created_at" => "0000-00-00 00:00:00"
  "updated_at" => "0000-00-00 00:00:00"
]
#original: array:10 [
  "id" => 1
  "date" => "2015-07-25"
  "start" => "12:30:00"
  "end" => "13:00:00"
  "fk_students_id" => 1
  "fk_teachers_id" => 1
  "lesson_array" => "[{"id":"1", "result":"0"},{"id":"2", "result":"1"},{"id":"15", "result":"2"}]"
  "slug" => "562153eae355e"
  "created_at" => "0000-00-00 00:00:00"
  "updated_at" => "0000-00-00 00:00:00"
]
#relations: array:2 [
  "student" => null
  "teacher" => null
]
#hidden: []
#visible: []
#appends: []
#fillable: []
#guarded: array:1 [
  0 => "*"
]
#dates: []
#casts: []
#touches: []
#observables: []
#with: []
#morphClass: null
+exists: true
}
]

Thank you for you help! 谢谢你的帮助!

As you see, 正如你看到的,

#relations: array:2 [
  "student" => null
  "teacher" => null
] 

Are both null, is because of foreign keys name. 两者均为null,是因为有外键名称。

So you should set them on the model: 因此,您应该在模型上设置它们:

public function student(){

    return $this->belongsTo('App\Student', 'fk_teachers_id', 'id');
}

public  function teacher(){

    return $this->belongsTo('App\Teacher', 'fk_students_id','id');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM