lavarel console command批处理文件

该文章展示了一个Laravel应用中使用phpartisan命令执行批处理操作的例子,具体是批量复制并注册商店的商品信息。通过Goods模型的chunkById方法,以每次处理10条数据的方式,高效地遍历并导入两千多条商品记录,整个过程大约耗时18分钟。
摘要由CSDN通过智能技术生成

一 面板执行命令
php artisan XX
比如 php artisan command:batch_register_shop

<?php

namespace App\Console\Commands;

use App\Libraries\Error;
use App\Libraries\Mysql;
use App\Libraries\Shop;
use App\Models\WholesaleCmSupplierTemp;
use App\Services\Goods\CopyGoodsService;
use Illuminate\Console\Command;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Logger;
use App\Models\MerchantsCategory;
use App\Models\Goods;
use App\Repositories\Common\BaseRepository;
class BatchRegisterShop extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'command:batch_register_shop';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '批量( 复制商品信息等)';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        
        echo '开始:' . date('Y-m-d H:i:s');
       
       
        $cm_srv = new CopyGoodsService();
        Goods::where('user_id', 179)->whereNotIn('goods_id',$arlist)->chunkById(10, function ($goods) use ($cm_srv) {
            foreach ($goods as $good) {
                $good = $good->toArray();
                $res = $cm_srv->importData($good['goods_id'],60);
            }
        });
       
        echo '结束:' . date('Y-m-d H:i:s');
        return ;
    }
  
}

二 分块处理数据

Goods::where( w h e r e ) − > c h u n k B y I d ( 10 , f u n c t i o n ( where)->chunkById(10, function ( where)>chunkById(10,function(goods) use (KaTeX parse error: Expected '}', got 'EOF' at end of input: … foreach (goods as $good) {
$good = $good->toArray();
$res = c m s r v − > i m p o r t D a t a ( cm_srv->importData( cmsrv>importData(good[‘goods_id’],60);
}
});
分块每次处理10条 整体处理完两千多商品的数据导入 大概用时 18分钟

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值