search

Home  >  Q&A  >  body text

PHP框架 Laravel Eloquent ORM 批量插入数据,怎么实现?

最近在使用Laravel。

想批量插入近千条数据,知道直接写sql是可以批量插入的。

另外框架ORM Eloquent 提供了一个Create方法:

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

<code>/**

     * Save a new model and return the instance.

     *

     * @param  array  $attributes

     * @return \Illuminate\Database\Eloquent\Model|static

     */

    public static function create(array $attributes)

    {

        $model = new static($attributes);

 

        $model->save();

 

        return $model;

    }

</code>

此方法可以很方便的插入数据,但是不能批量插入。
基于性能考虑吗,也不想写SQL,请问Eloquent 有针对这种情况的方法么?

PHPzPHPz2939 days ago917

reply all(1)I'll reply

  • 黄舟

    黄舟2017-04-10 14:47:58

    1

    2

    3

    4

    5

    <code>DB::table('users')->insert(array(

        array('email' => 'taylor@example.com', 'votes' => 0),

        array('email' => 'dayle@example.com', 'votes' => 0),

    ));

    </code>

    详情

    reply
    0
  • Cancelreply