简体   繁体   English

Laravel连接2个表,第一个表一个数据,第二个表多个数据

[英]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.

相关问题 如何从两个表中选择数据,而两个表只为第一表的一行选择了第二表的第一行 - How to SELECT data from two tables which only first row of second table is selected for one row of first table 从第一个表中选择一行,然后联接多个 - Select one row from first table then join multiple 将一行从一个mysql表连接到第二个表中的多行 - join one row from one mysql table to multiple row in second table Laravel:连接两个表从表一中获取信息,并从表二中获取第一个相关图像 - Laravel : Join two tables get the info from table one and get the first related image from table two Laravel Eloquent select 来自一个表,如果另一个表有匹配则加入第一行 - Laravel Eloquent select from one table and join first row if there's a match from another table 将表链接到第二个表并将数据从第一个表多次拉到第二个表 - Linking a table to a second table and pulling data from the first one to the second one multiple times mysql join查询从两个表中选择行,一个表有多个与第一个表匹配的行 - mysql join query for select rows from two tables that one table is having multiple rows matching to the first table 从两个表中选择,从第二个表中按一行排序 - select from two tables ordering by one row from second table 在一个提交上更新多个表将 id 从第一个表传递到第二个表 - Updating multiple tables on one submit passing id from first table to second 从3个表中检索数据如何在Laravel中使用第一个表从最后一个表中检索数据 - retrieve data from 3 tables How to retrieve data from the last table using the first one in Laravel
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM