【问题标题】:Error trying to pass variable to make a report in Excel尝试传递变量以在 Excel 中生成报告时出错
【发布时间】:2021-10-15 15:06:16
【问题描述】:

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

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

Unresolvable dependency resolving [Parameter #1 [ $cat ]] in class movieController

路线:

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

movieCatExport:

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');

    }

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

【问题讨论】:

  • 什么是 $cat 参考?是模特吗?
  • 你的路由不应该是/search/report/{cat}吗?你在那里缺少/
  • @A.Seddighi 当我按下报告按钮时,它会发送变量类别($cat)来制作报告
  • @TimLewis 不幸的是,错误仍然存​​在
  • 啊,我想你只需要从movieController 中删除你的__construct() 方法; Route 参数(如$cat)很可能没​​有传递给构造函数。结合在您的路线中添加/{cat} 应该可以工作。

标签: php excel laravel report maatwebsite-excel


【解决方案1】:

您应该指定 $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;
}    

【讨论】:

  • 很遗憾没用
  • 它是否在您的__construct() 以及export() 方法中指定?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-04-13
  • 1970-01-01
  • 2020-05-28
  • 2016-08-05
  • 1970-01-01
  • 1970-01-01
  • 2020-08-22
相关资源
最近更新 更多