Home > PHP Framework > Laravel > body text

Learn about batch insert operations in laravel framework in two minutes

烟雨青岚
Release: 2020-07-18 11:54:35
forward
8095 people have browsed it

Learn about batch insert operations in laravel framework in two minutes

In daily development, there are quite a lot of batch insertion operations. I remember a long time ago, I was still writing sql insertion in a loop, but was pressed to the ground and rubbed by the project manager. Well, performance is fine when not in use. If performance becomes a bottleneck, then code optimization and database optimization will bear the brunt.

Without further ado, let’s open the code!

1. First, check the laravel5.1 manual

Learn about batch insert operations in laravel framework in two minutes

##As can be seen from the picture: laravel provides the insert method of inserting an array, and That is to say, we can directly insert($array) to implement the batch insertion operation

2. First use laravel's get() query to obtain the array object, and then batch insert the operation

$ret = [];
    $create_red = new create_red();
    foreach($arr3 as $v){
      $delayDays = $v->delayDays;
      $workDays = $v->workDays;
      //获取当天时间戳的0点
      $now = strtotime(date('Y-m-d',time()));  ;
      $start = $now + $delayDays*86400;
      $start_at = date("Y-m-d H:i:s",$start);
      $end =  $now + $delayDays*86400 + $workDays*86400;
      $end_at = date('Y-m-d H:i:s',$end);
      $created_at = date("Y-m-d H:i:s",$now);
      $ret[] = [
        'uid'=>$uid,
        'status'=>1,
        'title'=>$v->title,
        'desc'=>$v->desc,
        'discount'=>$v->discount,
        'minprice'=>$v->minprice,
        'imgurl'=>$v->imgurl,
        'start_at'=>$start_at,
        'end_at'=>$end_at,
      ];
    }
    //往数据库批量插入数据
    $result = $create_red::insert($ret);
    if(!$result){
      DB::rollBack();
      return MyResponse::error(9006,'兑换优惠券失败');
    }
Copy after login

1. Create a new empty array

2. Loop through the

arr, and and loop towards emptyNumbergrouparr, and loop to the empty array4. Use the model::insert($array) method to insert in batches

5. The result is OK and the batch insertion is successful.

3. Batch insertion after generating redemption codes in batches

First enter the code:

 $num = 200;
    $codeArr = [];
    for($i=0;$i<$num;$i++){
      $code = EventCode::rand_str(8);
      $codeArr[$i][&#39;code&#39;] = $code;
    }
    /*var_dump($codeArr);
    exit;*/
    $event = new EventCode();
    $arr = $event::insert($codeArr);
    if(!$arr){
      return MyResponse::error(&#39;生成兑换码失败&#39;);
    }
Copy after login

1. Create a new empty Array 2. Loop to generate the redemption code and write it into the array

3. Print the array. After the array is displayed normally, use the insert() method to insert

4. Here The insertion effect is also OK. The efficiency of using laravel's own insert to achieve batch insertion is still acceptable. However, the number of insertions is small and the performance has not been specifically implemented. If I find anything in the future, I will write it down and share it with everyone.

Recommended tutorial: "

Laravel

"

The above is the detailed content of Learn about batch insert operations in laravel framework in two minutes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template