【发布时间】: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