如何在Laravel中传递命名路由中的参数

如何在Laravel中传递命名路由中的参数,laravel,Laravel,我想在我的刀片模板中传递url中的参数,url是: href="/finance/invoices/download/{{ $invoice->file_path }} 在web.php中,路由定义为: Route::get('invoices/download/{year}/{month}/{file}','InvoiceController@download')->name('download'); 并且该文件存储在数据库中,如下所示: 2018/07/invoiceberr

我想在我的刀片模板中传递url中的参数,url是:

href="/finance/invoices/download/{{ $invoice->file_path }}
在web.php中,路由定义为:

Route::get('invoices/download/{year}/{month}/{file}','InvoiceController@download')->name('download');
并且该文件存储在数据库中,如下所示:

2018/07/invoiceberry\u发票\u模板\u 1.docx


我应该怎么做?

您可以使用
route()
helper将参数传递给路由

它将第二个参数作为数组

route('download',[$year, $month, $file]);

希望这有助于

要添加到前面的答案中,请执行以下操作:

route('download',['year' => $year, 'month' => $month, 'file' => $file]);
你能做的很简单

{{ route('download', 1) }}
如果要传递多个参数,应将其作为数组传递,如下所示:

{{ route('download', ['year'=>1987]) }}
{{ route('download', ['year'=>1987, 'month'=>june, 'day'=>11]) }}

它将错误作为未定义变量:year(视图:C:\wamp64\www\Blog\employee portal\resources\views\finance\invoice\edit.blade.php)@lalitmehta,因为您没有将$year传递到edit.blade.php中,请检查控制器方法在我的控制器中,下载函数如下所示:公共函数下载($year,$month,$file,$inline=true){$headers=['content type'=>'application/pdf',];$file_path=FileHelper::getFilePath($year,$month,$file);if(!$file_path){return false;}if($inline){返回响应::make(存储::get($file_path),200,$headers)}返回Storage::download($file_path);}我的filehelper如下:公共静态函数getFilePath($year,$month,$file){$filePath=$year.'/'.$month.'/'.$file;如果(!Storage::exists($filePath)){return false;}返回$filePath;}@lalitmehta no,检查控制器方法,返回edit.blade.phpit的控制器方法将错误作为未定义变量:year(视图:C:\wamp64\www\Blog\employee portal\resources\views\finance\invoice\edit.blade.php)