PHP Laravel : 如何在将 xl/csv 导入 mysql 时避免重复数据?

PHP Laravel : How to Avoid duplicate data while importing xl/ csv into mysql?

我想将一个 csv 文件导入到数据库中,其中名称应该始终是唯一的,这意味着如果发现任何重复的标题,它应该避开该行并转到下一行。 我如何使用 Laravel 控制器来实现?

这是用于导入的控制器 csv/xl 我用过 :

 public function importExcel()
    {
        if(Input::hasFile('import_file')){
            $path = Input::file('import_file')->getRealPath();
            $data = Excel::load($path, function($reader) {
            })->get();
            if(!empty($data) && $data->count()){
                foreach ($data as $key => $value) {
                    $insert[] = ['title' => $value->title, 'description' => $value->description];
                }
                if(!empty($insert)){
                    DB::table('items')->insert($insert);
                  //  dd('Insert Record successfully.');
                }
            }
        }
        return back();    
    }

在将数据插入数据库之前,您可以对数组变量 $insert[] 应用 array_unique() 函数。它将 return 你独特的阵列。

或者使 table 中的列唯一,这样它就不能接受重复值。

我通常维护以前添加的项目的索引,如果我已经为它编制了索引,则跳过当前迭代(重新添加)。

在你的例子中,它应该是这样的:

 public function importExcel()
    {
        // Index titles
        $titles = [];

        if(Input::hasFile('import_file')){
            $path = Input::file('import_file')->getRealPath();
            $data = Excel::load($path, function($reader) {
            })->get();

            if(!empty($data) && $data->count()){
                foreach ($data as $key => $value) {

                    // Skip title previously added using array_key_exists or in_array
                    if (array_key_exists($value->title, $titles))
                        continue;

                    $insert[] = ['title' => $value->title, 'description' => $value->description];

                    // Index added title
                    $titles[$value->title] = true; // or array_push
                }

                if(!empty($insert)){
                    DB::table('items')->insert($insert);
                  //  dd('Insert Record successfully.');
                }
            }
        }
        return back();    
    }

对 Matt Borja 的 进行了一些改进。这还将检查来自 table.

的早期数据
public function importExcel()
{
    // Get current data from items table
    $titles = Item::lists('title')->toArray();

    if(Input::hasFile('import_file')){
        $path = Input::file('import_file')->getRealPath();
        $data = Excel::load($path, function($reader) {
        })->get();

        if(!empty($data) && $data->count()){
            $insert = array();

            foreach ($data as $key => $value) {
                // Skip title previously added using in_array
                if (in_array($value->title, $titles))
                    continue;

                $insert[] = ['title' => $value->title, 'description' => $value->description];

                // Add new title to array
                $titles[] = $value->title;
            }

            if(!empty($insert)){
                DB::table('items')->insert($insert);
              //  dd('Insert Record successfully.');
            }
        }
    }
    return back();    
}