完成后继续读取Maatwebsite Excel包中的所有行以用于laravel

时间:2016-08-11 17:21:45

标签: php excel laravel

我正在使用Laravel 5和Maatwebsite Excel软件包来读取和写入Excel文件,但我不确切知道如何管理下一个情况

public function readExcel(Request $request){
    $content = [];

    Excel::load('file.xls', function($reader){

        $content = $reader->get();
    });

    // do more somethings and return
    return $content;
}

问题是,当我返回$ content时,变量中没有任何内容,我知道$ content具有初始值且Excel :: load仍未完成且$ content的新值尚未设置。

感谢。

2 个答案:

答案 0 :(得分:1)

尝试在类中声明一个变量。 对于Ex:

[{"id":"2","0":"2","title":"sfgdg","1":"sfgdg","summary":"                         sdfgsadfg                  ","2":"                         sdfgsadfg                  ","content":"                     sdfgsdfg                      ","3":"                     sdfgsdfg                      ","main":"0","4":"0","keywords":"sdfgsdfg","5":"sdfgsdfg","author":"1","6":"1","order":null,"7":null,"friendly_url":"sdfgsdfgsdfg","8":"sdfgsdfgsdfg","date":"2016-08-09 19:51:37","9":"2016-08-09 19:51:37","active":"1","10":"1"},{"id":"3","0":"3","title":"test","1":"test","summary":"                               test             ","2":"                               test             ","content":"                            test                ","3":"                            test                ","main":"1","4":"1","keywords":"test","5":"test","author":"1","6":"1","order":null,"7":null,"friendly_url":"test","8":"test","date":"2016-08-09 19:52:00","9":"2016-08-09 19:52:00","active":"1","10":"1"},{"id":"4","0":"4","title":"test","1":"test","summary":"test               ","2":"test               ","content":"test                                            ","3":"test                                            ","main":"1","4":"1","keywords":"keyw","5":"keyw","author":"1","6":"1","order":null,"7":null,"friendly_url":"url_amigable","8":"url_amigable","date":"2016-08-10 11:31:50","9":"2016-08-10 11:31:50","active":"1","10":"1"},{"id":"5","0":"5","title":"test","1":"test","summary":"testt                                            ","2":"testt                                            ","content":"test                                            ","3":"test                                            ","main":"1","4":"1","keywords":"twat","5":"twat","author":"1","6":"1","order":null,"7":null,"friendly_url":"twet","8":"twet","date":"2016-08-11 08:37:23","9":"2016-08-11 08:37:23","active":"1","10":"1"},{"id":"6","0":"6","title":"test","1":"test","summary":"testt                                            ","2":"testt                                            ","content":"test                                            ","3":"test                                            ","main":"1","4":"1","keywords":"twat","5":"twat","author":"1","6":"1","order":null,"7":null,"friendly_url":"twet","8":"twet","date":"2016-08-11 08:37:29","9":"2016-08-11 08:37:29","active":"1","10":"1"},{"id":"7","0":"7","title":"test5345","1":"test5345","summary":"resumen                                            ","2":"resumen                                            ","content":"contenido                                            ","3":"contenido                                            ","main":"1","4":"1","keywords":"test","5":"test","author":"1","6":"1","order":null,"7":null,"friendly_url":"testtte","8":"testtte","date":"2016-08-11 08:46:36","9":"2016-08-11 08:46:36","active":"1","10":"1"}]

另一种方式:

class TestController extends Controller {
   public $content = [];

   public function readExcel(Request $request){

    Excel::load('file.xls', function($reader){

        $this->content = $reader->get();
    });

    // do more somethings and return
    return $this->content;
}

}

答案 1 :(得分:0)

您没有返回关闭内容。试试这个:

public function readExcel(Request $request)
{
    $content = Excel::load('file.xls', function($reader) {
        return $reader->get();
    });

    // do more somethings and return
    return $content;
}