Laravel Excel验证 [英] Laravel Excel validation

查看:750
本文介绍了Laravel Excel验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经为laravel安装了maatwebsite/excel软件包.它工作正常.我的问题是检查文件后如何处理.

i have installed the maatwebsite/excel package for laravel. It works fine. My problem is how to handle it after i check the file.

我在其中显示文件论坛以及将文件上传到哪里的upload()函数在这里:

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

upload_excel_files函数检查文件是否具有正确的格式,如果是,则对其进行处理,并且必须返回成功的注释.如果文件与require格式不匹配,则需要提供错误消息.

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.

不幸的是,我不确定将用户消息放在何处.如示例中一样,它总是返回成功的消息.如果由于某种原因消息在upload_excel_files()函数中,但我没有通过第一个重定向获得消息,则在第二个重定向中得到了消息,这很奇怪.

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.

你们会推荐我做什么?

推荐答案

使用laravel请求检查文件格式是否正确

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',
        ];
    }

更多的mime类型: https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types

More mime-type: https://svn.apache.org/repos/asf/httpd/httpd/trunk/docs/conf/mime.types

有关错误消息,请阅读以下内容: https://laravel.com/docs/5.4/validation#working-错误消息

For errors message read this: https://laravel.com/docs/5.4/validation#working-with-error-messages

这篇关于Laravel Excel验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆