Laravel excel 传递视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30319555/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 11:35:08  来源:igfitidea点击:

Laravel excel passing a view

phpexcellaravel

提问by Loko

I'm trying to export data to excel and I can't seem to make it work. I am using: http://www.maatwebsite.nl/laravel-excel/docs

我正在尝试将数据导出到 excel,但似乎无法正常工作。我正在使用:http: //www.maatwebsite.nl/laravel-excel/docs

and I'm following the docs as much as I can. So I have this view which I'm trying to get in excel. The docsare telling me:

我正在尽可能多地关注文档。所以我有这个视图,我试图在 excel 中获得它。该文档告诉我:

Loading a view for a single sheet

加载单个工作表的视图

$sheet->loadView('folder.view');

So I did that, and (of course) I get an error saying that the variables in the views are not declined since I'm not passing the variables into the view. So I read the docs again and it says:

所以我这样做了,并且(当然)我收到一个错误,说视图中的变量没有被拒绝,因为我没有将变量传递到视图中。所以我再次阅读了文档,它说:

Passing variables to the view

将变量传递给视图

Alternatively you can use the with() method which works the same as with Laravel views.

或者,您可以使用与 Laravel 视图相同的 with() 方法。

with a basic with()example.

用一个基本的with()例子。

So now I have this in my controller function:

所以现在我的控制器功能中有这个:

public function something(){
$something2='';
$something='';
Excel::create('Laravel Excel', function($excel) {

    $excel->sheet('Excel sheet', function($sheet) {
        $sheet->loadView('something')->with('something',$something)
                                     ->with('something2',$something2);
        $sheet->setOrientation('landscape');
    });

})->export('xls');
}

now I get an error in the controller:

现在我在控制器中收到一个错误:

undefined variable $something

未定义的变量 $something

So I didn't know what could be going wrong. I remained positive and thought it would be something easy like $something actually not being declared, so I removed that from the excel exporting. Then it gave me the error:

所以我不知道会出什么问题。我仍然保持积极的态度,并认为这会很容易,比如实际上没有声明 $something,所以我从 excel 导出中删除了它。然后它给了我错误:

undefined variable $something2

未定义的变量 $something2

After this I knew there was something else wrong. Since both aren't 'defined' according to the excel creation. So I tried out something new. Declaring both variables inside of the excel creation. Al though there is more code between the declaring of the variables and the excel creation. So this wouldn't work. So the code is like this:

在此之后,我知道还有其他问题。由于两者都不是根据 excel 创建“定义”的。所以我尝试了一些新的东西。在 excel 创建中声明两个变量。尽管在声明变量和创建 excel 之间有更多的代码。所以这行不通。所以代码是这样的:

$something2='';
$something='';
//more code
if($check==true){
Excel::create('Laravel Excel', function($excel) {

So I'm still stuck.

所以我还是卡住了。

What is going wrong here? Am I misunderstanding this excel creation stuff?

这里出了什么问题?我误解了这个 excel 创作的东西吗?

采纳答案by oBo

I think this might be the problem, since you're using cloasures, the variables is not accessable in the cloasure without defining the use of them.

我认为这可能是问题所在,因为您使用的是 cloasure,如果不定义它们的使用,则无法在 cloasure 中访问变量。

public function something(){

    $something2='';
    $something='';

    Excel::create('Laravel Excel', function($excel) use ($something, $something2) {

        $excel->sheet('Excel sheet', function($sheet) use ($something, $something2) {
            $sheet->loadView('something')->with('something',$something)
                                         ->with('something2',$something2);
            $sheet->setOrientation('landscape');
        });

    })->export('xls');

}