Laravel Excel validation

好久不见. 提交于 2019-12-23 03:24:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!