首页 > 解决方案 > 尝试传递变量以在 Excel 中生成报告时出错

问题描述

我正在尝试传递变量cat来搜索 DB 中的相关数据并在 excel 中生成报告。

不幸的是,我收到以下错误:

类movieController中无法解析的依赖解析[Parameter #1 [ $cat ]]

路线:

Route::get('/search/report{cat}', [movieController::class , 'export'])->name('search-report');

电影猫出口:

class movieCatExport implements FromCollection, Responsable, WithHeadings, ShouldAutoSize{
    use Exportable;
    //private $fileName='usertest.csv';
    /**
    * @return \Illuminate\Support\Collection
    */

   

    public function collection(){     
        $cat=$this->cat; 
        $film=film::with('category')->wherehas('category', function($q) use($cat){
            $q->where('name', $cat);
              })->join('semantic', 'semantic.idtable_record','=','film.film_id')
->join('language', 'language.language_id','=','film.language_id')->GET();
    
            return $film;
    }
  
    public function headings(): array{
        return [
            'title',
            'description',
            'year',
            'duration(min.)',
            'language',
            'features',
            'onto',
            'class',
            'proprierty'
                
            ];
        }

电影控制器:

class movieController extends Controller{
    protected $table='film';
    private $excel;
    private $cat;
   
    public function __construct(Excel $excel, $cat){
        $this->excel =$excel;
        $this->cat=$cat;
    }    

    public function export(Excel $excel,string $cat){
            return $this->excel->download(new movieCatExport($cat), 'test.csv');
    }
}

注意:删除了 __construct 函数,出现以下错误:

在 null 上调用成员函数 download()

public function export(Excel $excel,string $cat){

            return $this->excel->download(new movieCatExport($cat), 'test.csv');

    }

注意2:我从$this->excel更改为$excel->download注意3 :我发现该集合没有收到$cat,现在我需要解决这个问题

标签: phpexcellaravelreportmaatwebsite-excel

解决方案


您应该指定$cat控制器中的类型

当它是一个字符串

public function __construct(Excel $excel, string $cat){
    $this->excel = $excel;
    $this->cat = $cat;
}    

或者当它是一个模型时:

public function __construct(Excel $excel, Cat $cat){
    $this->excel = $excel;
    $this->cat = $cat;
}    

推荐阅读