【问题标题】:Laravel Join 2 tables , one data from first table and multiple row from second tableLaravel 连接 2 个表,第一个表中的一个数据和第二个表中的多行
【发布时间】:2018-05-17 15:38:48
【问题描述】:

第一个表

标识 |姓名 |


1 |学生1 |

2 |学生2 |

3 |学生3 |

4 |学生4 |

================================

第二张表

标识 |螺柱ID |主题 |标记


1 | 1 |数学 | 50

2 | 1 |英语 | 45

3 | 2 |数学 | 20

4 | 3 |数学 | 40

我将如何使用上述表结构在 laravel 中查询.. 我需要输出作为

{

    "id": 1,

    "name":"student1"

    "marks":[

        {

            "subject":"maths",

            "marks":50,

        },

        {

            "subject":"emglish",

            "marks":45,

        }

    ]

}

【问题讨论】:

  • 你想使用 laravel 查询构建器还是 laravel eloquent ORM 查询?
  • 一切都好:)

标签: php mysql json laravel-5 laravel-query-builder


【解决方案1】:

我已经在 Laravel 查询生成器中完成了。 请看下文。

$stud = DB::table('FirstTable')
               ->join('SecondTable','FirstTable.id','=','SecondTable.stud_id')
               ->where('FirstTable.id','=','1')
               ->get();
dd($stud);

【讨论】:

    【解决方案2】:

    我以 laravel 雄辩的方式来做这件事。在您的学生模型中创建关系方法。是一个学生可以有很多学科关系

    public function subject(){
    return $this->hasMany('App\"the second table model name",'stud_id');
    

    }

    然后你可以在控制器中访问它

    public function index(){
    $student = Student::find("Student_id")->subject;
    dd($student);
    }
    

    请阅读文档以获得更好的理解 https://laravel.com/docs/5.5/eloquent-relationships

    【讨论】:

      【解决方案3】:

      您可以创建与这些表的关系。在这里你需要一对多的关系。

      你需要学生、课桌

      学生

      Schema::create('students', function (Blueprint $table) {
      $table->increments('id');
      $table->string('student_name')
      });
      

      课程

      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')
      });
      

      然后编辑你的模型如下

      学生模型;

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

      在你的课程模型中;

       public function students(){
      
            return $this->belongsTo('App\Student');
          }
      

      然后在你的控制器中,

      $students = Student::whereHas('lessons', function ($query) {
      
                  $query->where("exam_number", 2 (or which exam));
              })->get();
      

      最后,在你的刀片中;

       <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> 
      

      这应该可行

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-01-18
        • 2012-08-13
        • 2018-05-05
        • 1970-01-01
        • 2017-09-19
        • 2021-10-13
        • 2020-01-04
        • 1970-01-01
        相关资源
        最近更新 更多