如何在Laravel中导入期间跳过csv文件中的空行

如何在Laravel中导入期间跳过csv文件中的空行,laravel,csv,import,Laravel,Csv,Import,在导入CSV文件期间,my import函数为导入文件的每一行创建一个数据库记录。这意味着,如果用户在文件中留下了一个空行,那么导入过程将在数据库中创建一个空白记录。如何开发它,以便导入程序识别CSV文件中的空白/空行,跳过这些空白/空行,并且仅在该行包含数据时创建记录并导入行 以下是导入功能的控制器: public function process(Request $request) { $filename = $request->input('filename'

在导入CSV文件期间,my import函数为导入文件的每一行创建一个数据库记录。这意味着,如果用户在文件中留下了一个空行,那么导入过程将在数据库中创建一个空白记录。如何开发它,以便导入程序识别CSV文件中的空白/空行,跳过这些空白/空行,并且仅在该行包含数据时创建记录并导入行

以下是导入功能的控制器:

public function process(Request $request)
    {
        $filename = $request->input('filename', false);
        $path = storage_path('app/csv_import/'.$filename);

        $hasHeader = $request->input('hasHeader', false);
        $isAddingNewReligiousBackground = $request->input('is_adding_new_religious_background', false);

        $fields = $request->input('fields', false);
        $fields = array_flip(array_filter($fields));

        $modelName = $request->input('modelName', false);
        $model = 'App\\'.$modelName;

        $reader = new SpreadsheetReader($path);
        $insert = [];
        $update = [];
        $tags = [];

        $team_id = $request->input('church_id');
        $custom_fields = CustomField::where('created_by_team_id', $team_id)->get();

        foreach ($reader as $key => $row) {
            if ($hasHeader && $key == 0) {
                continue;
            }

            $tmp = [];
            $meta = [];
            foreach ($fields as $header => $k) {
                $tmp[$header] = $row[$k];
            }

            $tmp['created_by_id'] = auth()->user()->id;
            $tmp['created_by_team_id'] = $team_id;
            $tmp['created_at'] = now();

            if ($modelName == 'Interest') {
                $this->translateReligiousBackgroud($tmp, $isAddingNewReligiousBackground);
                $meta = $this->processInterestMeta($tmp, $custom_fields);
                $existing = Interest::matchingInterest((object) $tmp, $request->input('church_id'));
                if ($existing) {
                    $update[] = $existing;
                    $tagsArr = array_filter((array) $request->input('interest_tags'));
                    foreach (\DB::table('interest_tag')->where('interest_id', $existing->id)->get() as $tag) {
                        $tagsArr[] = $tag->tag_id;
                    }
                    $existing->interest_tags()->sync(array_filter($tagsArr));
                    if (! empty($meta)) {
                        $existing->syncMeta($meta);
                    }
                } else {
                    $tmp['meta'] = $meta;
                    $insert[] = $tmp;
                }
            } else {
                $insert[] = $tmp;
            }
        }

        $for_insert = array_chunk($insert, 10000); // this was 100, but the chunking was causing the system to only import the first 100 records, even though the success message said it imported all of them - LW 1/25/2019
        foreach ($for_insert as $insert_item) {
            if ($modelName == 'Interest') {
                foreach ($insert_item as $item) {
                    $interest = new $model;
                    foreach ($item as $field => $value) {
                        if ($field != 'meta') {
                            $interest->$field = $value;
                        }
                    }
                    $interest->created_by_id = auth()->user()->id;
                    $interest->created_by_team_id = $request->input('church_id');
                    $interest->save();

                    // For some reason, created_by_team_id was null on initial save, do it again
                    $interest->created_by_team_id = $request->input('church_id');
                    $interest->save();

                    // deal with tags
                    $interest->interest_tags()->sync(array_filter((array) $request->input('interest_tags')));

                    // deal with custom fields
                    if (! empty($item['meta'])) {
                        $interest->syncMeta($item['meta']);
                    }
                }
            } else {
                $model::insert($insert_item);
            }
        }
        $rows = count($insert);
        $updates = count($update);
        $table = Str::plural($modelName);

        File::delete($path);

        $redirect = $request->input('redirect', false);

        return redirect()->to($redirect)->with('message', trans(($updates > 0 ? 'global.app_imported_rows_to_table_with_updates' : 'global.app_imported_rows_to_table'),
            ['rows' => $rows, 'updates' => $updates, 'table' => $table]
        ));
    }

我不相信这是正确的方法,我希望有人会出现,并给出正确的答案,但这是我的肮脏的黑客。在我的数据集中,我的lastname有一列,所有具有任何功能的记录都必须有一个姓氏,因此我在保存前放置了一个-if-语句
if(!empty($interest['lastname']))
检查“lastname”是否为空

         $for_insert = array_chunk($insert, 10000); // this was 100, but the chunking was causing the system to only import the first 100 records, even though the success message said it imported all of them - LW 1/25/2019
        foreach ($for_insert as $insert_item) {
            if ($modelName == 'Interest') {
                foreach ($insert_item as $item) {
                    $interest = new $model;
                    foreach ($item as $field => $value) {
                        if ($field != 'meta') {
                            $interest->$field = $value;
                        }
                    }
                    $interest->created_by_id = auth()->user()->id;
                    $interest->created_by_team_id = $request->input('church_id');
                    if (!empty($interest['lastname']))
                    {
                        $interest->save();
                    }
                    // For some reason, created_by_team_id was null on initial save, do it again
                    $interest->created_by_team_id = $request->input('church_id');
                    if (!empty($interest['lastname']))
                    {
                        $interest->save();
                    } 
因此,通过这种方式,系统将完成导入csv中所有行的过程,无论它们是否有数据,但它只保存那些具有lastname值的记录。这满足了我的需要,但我知道必须有一个更干净的方法来做到这一点


我认为应该有这样一种方法,只有当行/记录中的所有值都为空时,才应跳过记录。因此,如果有一个值,它将插入并创建一个条目,但如果一行中的所有值都为空,它将跳过条目。希望有人会在某个时候出现,并提供更雄辩的解决方案

为什么不检查要插入DB的foreach循环中的字段是否为空?是的,谢谢,很抱歉不清楚。这就是我正在尝试做的,但对我来说没有用,这就是我希望得到帮助的地方。