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

时间:2016-08-10 05:16:04

标签: php mysql laravel laravel-5 import-from-excel

我想将csv文件导入数据库,其中name应该始终唯一意味着如果找到任何重复的标题,它应该避开该行并转到下一行。 如何使用Laravel Controller实现?

这是我用过的导入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();    
    }

3 个答案:

答案 0 :(得分:2)

在将数据插入数据库之前,可以在数组变量$ insert []上应用array_unique()函数。它会返回唯一的数组。

或者使表中的列唯一,以便它不能接受重复值。

答案 1 :(得分:1)

Matt Borja answer的一些改进。这也将检查表中的早期数据。

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();    
}

答案 2 :(得分:0)

我通常维护以前添加的项目的索引,如果我已将其编入索引,则跳过当前迭代(重新添加)。

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

 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();    
    }