【问题标题】:How to solve failed job batching in laravel如何解决 laravel 中失败的作业批处理
【发布时间】:2021-07-25 15:59:09
【问题描述】:

我做了一个作业批处理,将 csv 插入数据库。

方法如下:

public function upload()
{
    if(request()->has('file')){
        $data = file(request()->file);
        
        // Chunking file
        $chunks = array_chunk($data,5);
       
        $batch = Bus::batch([])->dispatch();

        foreach($chunks as $chunk){
            
            $data = array_map('str_getcsv',$chunk);

            $batch->add(new BinlocCsvProcess($data));
        } 

        return $batch;
    }
    return 'please upload file';
}

这是我的工作课程:

class BinlocCsvProcess implements ShouldQueue {

public $data;
/**
 * Create a new job instance.
 *
 * @return void
 */
public function __construct($data)
{
    $this->data = $data;
}

/**
 * Execute the job.
 *
 * @return void
 */
public function handle()
{
    foreach($this->$data as $item){
        Binloc::create($item);
    } 
}
}

我不知道如何得到错误,为什么它不起作用,我不知道如何解决它。

请帮帮我。

【问题讨论】:

  • 你能指定错误是什么>
  • 我不知道如何得到错误。当我运行 php artisan queue:work 时,所有过程都失败了
  • 我得到了错误。 ErrorException:未定义变量:/binloc/app/Jobs/BinlocCsvProcess.php:36 中的数据
  • 创建一个新的作业实例。来自构造函数中的类
  • 问题解决了。在 handle() 方法中应该是 $this->data as $item ,而不是 $this->$data as $item。谢谢@JEJ

标签: laravel file-upload queue chunks job-batching


【解决方案1】:

出现错误是因为行 foreach($this->$data as $item)。你应该删除$,所以你的代码应该是这样的foreach($this->data as $item)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-22
    • 2018-08-16
    • 1970-01-01
    • 1970-01-01
    • 2012-07-18
    • 2022-01-07
    相关资源
    最近更新 更多