Ich versuche, eine Datei zu validieren, die ich über ein Formular in meine DB einfügen möchte. Die Datei sollte "csv" sein und ihr Inhalt muss ebenfalls validiert werden. HierLaravel: Importieren von CSV - Validierung
ist die Import-Methode in der Steuerung, die die Form behandelt:
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;
}
Die Validate-Methode:
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;
}
}
Der Fehler Ich habe:
Error in QuoteService. php line 123: Argument 1 wurde an übergeben App \ Services \ QuoteService :: validate() muss vom Typ array, 0123, seinObjekt gegeben, genannt in /var/www/html/Acadia/app/Services/QuoteService.php auf der Leitung 233 und
definiert
Bitte ..post den Code Ihrer Methode 'validate'. Es erwartet ein Array. aber "data" ist das geladene Excel-Objekt – cmnardi
öffentliche Funktion validate (array $ data) { $ this-> validator = Validator :: make ($ data, $ this-> rules, $ this-> messages); if ($ this-> validator-> fehlgeschlagen()) { $ exception = new InvalidDataException(); $ Fehler = $ this -> _ parseMessages(); $ exception-> setErrors ($ errors); throw $ Ausnahme; } } –
Ich denke, das Problem ist, dass Sie versuchen, einen Validator zu verwenden, um das Excel-Objekt zu validieren, aber dieser Validator erwartet, Formularfelder zu validieren .; Hast du das schon einmal versucht? http://StackOverflow.com/Questions/23625672/Laravel-File-Upload-validation – cmnardi