[英]Laravel Join 2 tables , one data from first table and multiple row from second table
FirstTable FirstTable
id | id | name | 名称|
1 | 1 | student1 | 学生1 |
2 | 2 | student2 | 学生2 |
3 | 3 | student3 | 学生3 |
4 | 4 | student4 | 学生4 |
============================== ==============================
SecondTable SecondTable
Id | ID | stud_id | stud_id | subject | 主题| mark 标记
1 | 1 | 1 | 1 | maths | 数学| 50 50
2 | 2 | 1 | 1 | english | 英语| 45 45
3 | 3 | 2 | 2 | maths | 数学| 20 20
4 | 4 | 3 | 3 | maths | 数学| 40 40
How would i query in laravel with the above table structure.. i need outpus as 我将如何使用上面的表结构在laravel中查询..我需要outpus作为
{
"id": 1,
"name":"student1"
"marks":[
{
"subject":"maths",
"marks":50,
},
{
"subject":"emglish",
"marks":45,
}
]
}
I have done it in Laravel Query Builder. 我已经在Laravel查询生成器中做到了。 Please see below. 请看下面。
$stud = DB::table('FirstTable')
->join('SecondTable','FirstTable.id','=','SecondTable.stud_id')
->where('FirstTable.id','=','1')
->get();
dd($stud);
I'm doing it in laravel eloquent way. 我正在以雄辩的方式做这件事。 In your Student model create relationship method. 在学生模型中创建关系方法。 It is a student can have many subject relationship 这是一个学生可以有很多学科关系
public function subject(){
return $this->hasMany('App\"the second table model name",'stud_id');
} }
then you in controller you can access it like 那么您在控制器中可以像访问它一样
public function index(){
$student = Student::find("Student_id")->subject;
dd($student);
}
please read the documentation for better understanding https://laravel.com/docs/5.5/eloquent-relationships 请阅读文档以更好地了解https://laravel.com/docs/5.5/eloquent-relationships
You can create a relationships with these tables. 您可以使用这些表创建关系。 In here you need that one to many relationships. 在这里,您需要一对多的关系。
You need students , lessons tables 您需要学生,课程表
Students 学生们
Schema::create('students', function (Blueprint $table) {
$table->increments('id');
$table->string('student_name')
});
Lessons 教训
Schema::create('lessons', function (Blueprint $table) {
$table->increments('id');
$table->integer('student_id');
$table->integer('exam_number'); // there will be many exams right ?
$table->integer('lesson1')
$table->integer('lesson2');
$table->integer('lesson-etc');
$table->timestamps();
$table->foreign('student_id')->references('id')->on('students')
});
Then edit your models as below 然后如下编辑模型
Student model; 学生模型;
public function lessons(){
return $this->hasMany('App\Lesson','student_id','id');
}
and in your lessons model; 并在您的课程模型中;
public function students(){
return $this->belongsTo('App\Student');
}
Then in your controller, 然后在您的控制器中
$students = Student::whereHas('lessons', function ($query) {
$query->where("exam_number", 2 (or which exam));
})->get();
Finally, in your blade; 最后,在您的刀片中;
<table >
<tr>
<th>Student Name</th>
<th>Lesson 1</th>
<th>Lesson 2</th>
</tr>
@foreach($students as $student)
<tr>
<td>{{$student->student_name}}</td>
<td>{{$student->lessons->lesson1}}</td>
<td>{{$student->lessons->lesson2}}</td>
</tr>
@endforeach
</table>
This should be work 这应该是工作
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.