Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel:导入CSV-验证_Php_Laravel_Validation_Csv_Maatwebsite Excel - Fatal编程技术网

Php Laravel:导入CSV-验证

Php Laravel:导入CSV-验证,php,laravel,validation,csv,maatwebsite-excel,Php,Laravel,Validation,Csv,Maatwebsite Excel,我试图通过表单验证要插入数据库中的文件。该文件应为“csv”,其内容也应进行验证 以下是控制器中处理表单的导入方法: public function importFromCsv(array $data) { if (Input::hasFile('import_file')) { $path = Input::file('import_file')->getRealPath(); $data = Excel::load($path, functio

我试图通过表单验证要插入数据库中的文件。该文件应为“csv”,其内容也应进行验证

以下是控制器中处理表单的导入方法:

public function importFromCsv(array $data) {
    if (Input::hasFile('import_file')) {
        $path = Input::file('import_file')->getRealPath();

        $data = Excel::load($path, function($reader) {
            //...
        })->get();

        $this->validator = new QuoteValidator();

        $this->validate($data);

        if (!empty($data) && $data->count()) {

            foreach ($data as $key => $value) {
                $insert[] = [
                    'content' => $value->content, 
                    'created_at' => $value->created_at,
                    'updated_at' => $value->created_at
                ];
            }

            if (!empty($insert)) {
                DB::table('quotes')->insert($insert);
            }
        }
    }
    return true;
}
验证方法:

public function validate(array $data) {
    $this->validator = Validator::make($data, $this->rules, $this->messages);

    if ( $this->validator->fails() ) {
        $exception = new InvalidDataException();
        $errors = $this->_parseMessages();
        $exception->setErrors($errors);
        throw $exception;
        }
}
我得到的错误是:

QuoteService.php第123行中的ErrorException:参数1传递给 App\Services\QuoteService::validate()必须是数组类型, 对象已给定,已调用 /第233行的var/www/html/Acadia/app/Services/QuoteService.php和 明确的


传递给validate方法的变量是集合对象(),但validate方法需要一个数组

在将数据传递给validate方法之前,必须将其转换为数组。您可以通过调用方法
toArray()

例如:

$data = Excel::load($path, function($reader) {
     //...
})->get()->toArray();

传递给validate方法的变量是集合对象(),但validate方法需要一个数组

在将数据传递给validate方法之前,必须将其转换为数组。您可以通过调用方法
toArray()

例如:

$data = Excel::load($path, function($reader) {
     //...
})->get()->toArray();

请..发布方法的代码
validate
。它期望收到一个数组。但是
data
是加载的Excel objectpublic函数validate(数组$data){$this->validator=validator::make($data,$this->rules,$this->messages);if($this->validator->fails()){$exception=new InvalidDataException();$errors=$this->\u parseMessages();$exception->setErrors($errors);throw$exception;}}}我认为问题在于您试图使用验证器验证Excel对象,但该验证器希望验证表单字段。;你试过这种方法吗?请..发布方法的代码
validate
。它期望收到一个数组。但是
data
是加载的Excel objectpublic函数validate(数组$data){$this->validator=validator::make($data,$this->rules,$this->messages);if($this->validator->fails()){$exception=new InvalidDataException();$errors=$this->\u parseMessages();$exception->setErrors($errors);throw$exception;}}}我认为问题在于您试图使用验证器验证Excel对象,但该验证器希望验证表单字段。;你试过这种方法吗?结果:在调用$data->count()的if语句中,对非对象调用成员函数count()。现在,您的数据是一个数组。您必须将其更改为count($data)。结果:在调用$data->count()的if语句中,对非对象调用成员函数count()。现在,您的数据是一个数组。您必须将其更改为count($data)。