php - 尝试传递变量以在 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,现在我需要解决这个问题
解决方案
您应该指定$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;
}
推荐阅读
- python - 如何让我的网站“联系我”表格到达我的 Gmail?
- reporting-services - Power BI Embedded:在 RLS 中使用哈希值?
- c - 在 C 中读取输入文件
- json - 结构中的 json 标记真的需要编组吗?
- git - Git避免通过本地配置将新分支推送到远程
- r - 使包含要重新绑定的数据帧的新变量的更快方法
- amazon-lightsail - 无法通过 SSH 连接到从快照创建的新 Lightsail 实例
- html - HTML 表单输入创建数组数组
- javascript - require() 和 $ 未在电子中定义
- c++ - 从接口的父接口到哪里实现功能?