问题
i have installed the maatwebsite/excel package for laravel. It works fine. My problem is how to handle it after i check the file.
The upload() function in which i show to file forum and where i upload the file is here:
public function upload(Request $request) {
$orientation = $request->get('orientation');
if($request->file('file')) {
$status = Excel::load(Input::file('file'), function ($reader) use ($orientation) {
return self::upload_excel_files($reader, $orientation);
});
if($status) {
Session::flash('status', "Excel data was imported successfully");
return redirect(route('admin.exchange.upload'));
}
else {
return redirect(route('admin.exchange.upload'))->withErrors('Invalid format. Please check the XLS template.');
}
}
return view('admin.exchange.upload');
}
public function upload_excel_files($file, $orientation) {
if(something) return true;
else return false;
}
The upload_excel_files function checks if the file has the right format and if yes it handles it and must return a successful note. If the file does not match the require format it needs to give an error message.
Unfortunately i am not sure where to put the user messages. As in the example it always return a successful message. If the messages are in the upload_excel_files() function for some reason i don't get the message by the first redirect, i get it at the second redirect which is strange.
What would u guys recommend me to do ?
回答1:
For checks if the file has the right format use laravel request
public function rules()
{
return [
'file' => 'required|mimeTypes:'.
'application/vnd.ms-office,'.
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,'.
'application/vnd.ms-excel',
];
}
More mime-type: https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types
For errors message read this: https://laravel.com/docs/5.4/validation#working-with-error-messages
来源:https://stackoverflow.com/questions/37769433/laravel-excel-validation