【问题标题】:How to get data in database如何获取数据库中的数据
【发布时间】:2020-04-02 19:21:27
【问题描述】:

您好,我是 laravel 新手,我想实现以下目标

  • 我在下面有 2 个表,即 table1 和 table2
table1 = students[{id="1", firstname="john", lastname="doe"},
                  {id="2", firstname="james", lastname="bond"}]
table2 = subjects[{id="1", code="math101", subject="math", description="Introduction", units="3"},
                   id="2", code="eng101", subject="English", description="introduction", units="3"]
  • 现在在 laravel 控制器中我想实现以下目标
public function create(){
 //lets assume that i received these id's from the routes
 // i want to use this id's to filter the data i want to add to the table3
  id = 2 //students
  id = 1 //subjects

table3 = enroll[{id="1" student_id="2", firstname="john", lastname="doe", code="math101", subject="math", description="Introduction", units="3"}]

我想合并这两个表中的数据并将其保存到新表中,这样它就会像上面的表 3 中那样

如何在 laravel 控制器中实现这一点

提前致谢

【问题讨论】:

    标签: php laravel-6


    【解决方案1】:

    你必须在 Laraval 框架中使用 Eloquent ORM 来实现这一点。首先,您需要使用artisan 命令行为数据库中的每个表创建模型,然后您可以使用 ORM 进行数据库查询。

    您可以在以下线程中了解有关在 laraval 中创建模型的更多信息。

    How do I create a model in laravel?

    然后你就可以按照控制器里面雄辩的文档进行查询了。

    https://laravel.com/docs/master/eloquent

    【讨论】:

      猜你喜欢
      • 2018-07-14
      • 1970-01-01
      • 2013-03-23
      • 1970-01-01
      • 2012-11-20
      • 2020-10-31
      • 2021-02-04
      相关资源
      最近更新 更多