【问题标题】:How to execute different queries same time in Laravel如何在 Laravel 中同时执行不同的查询
【发布时间】:2019-05-05 12:39:30
【问题描述】:

我想将数据插入到不同的表中,并在一个连接中选择一些数据表单表。看起来像:

  DB::connection()->enableQueryLog();

    $query1 = "insert into emails(`email`) values('i.babirli@outlook.csosm');";
    $query2 = "insert into users(`name`,`email`,`password`) values('Ismat','i.babirlis@outlook.com','123456');";
    $query3 = "select name from users where id=1;";


    DB::unprepared( $query1.$query2.$query3);
    $queries = DB::getQueryLog();

但是有一个未准备好的方法,它不会返回我选择的数据。有什么办法吗?谢谢你的回答。

【问题讨论】:

    标签: php mysql sql database laravel


    【解决方案1】:

    您可以使用 DB::unprepared() 进行多次插入,但无法获得选择查询的结果。 在这个函数的定义中它调用 PHP PDO

    $this->getPdo()->exec($query) 
    

    和 PHP doc,明确声明它只会返回 true 或 false,并建议对 select 语句使用查询。

    所以,你可以在这里做的是,在单个命令中执行多个插入,并选择使用查询功能或 eloquent。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-06-25
      • 2021-07-22
      • 2017-01-28
      • 2021-09-23
      • 1970-01-01
      • 1970-01-01
      • 2019-01-26
      相关资源
      最近更新 更多