【问题标题】:Laravel - How to get the inputs and pass it from one view to another view (Mpdf)Laravel - 如何获取输入并将其从一个视图传递到另一个视图 (Mpdf)
【发布时间】:2019-12-20 08:38:40
【问题描述】:

我的报告视图中有一个日期范围过滤器,因此当用户单击下载按钮时,它应该将选定的日期传递给 pdf 视图。

默认日期是当月第一天到今天为止,用户可以根据需要过滤日期。

如何将这些输入传递给另一个视图?

我应该从 index 和 filter 函数中传递 $from 和 $to 变量。

我尝试了很多方法,但没有一个有效:(

有什么建议吗?

这是代码:(

控制器

 public function index(Request $request){
 $startDate = Carbon::now();
        $from = $startDate->firstOfMonth()->format('Y-m-d');
        $to = Carbon::now()->format('Y-m-d');

 return view('Reports.index',compact('from','to');
}

 public function filter(Request $request)
    {
        $from = Carbon::parse($request->input('datepicker_from'))->format('Y-m-d');
        $to = Carbon::parse($request->input('datepicker_to'))->format('Y-m-d');

  return view('Reports.index',compact('from','to');
}

public function print()
    {
        $pdf = PDF::loadView('Reports.reportPDF');
        return $pdf->stream('my.pdf', array('Attachment' => 0));
    }

网络

Route::get('print', 'reportController@print')->name('Reports.reportPDF');

观看次数

<a href="{{ route('Reports.reportPDF') }}" target="_blank"><button class="btn btn-success" title="Download PDF" data-toggle="tooltip"><i class="fa fa-download"></i></button></a>

【问题讨论】:

  • 你使用Barryvdh\DomPDF ??
  • 不,我正在使用 Meneses\LaravelMpdf\Facades\LaravelMpdf
  • 将过滤参数传递给打印函数或创建一个同时用于filter()和print()的过滤函数
  • @Kamran 你可以在回答部分提供任何例子
  • @Suvin94 类似于this 并在打印链接或锚点中作为 URL 查询传递给和传递

标签: php laravel session input mpdf


【解决方案1】:

将这两个变量传递给 PDF 视图。

public function print($from,$to)
    {
        $pdf = PDF::loadView('Reports.reportPDF',['from'=>$from,'to'=>$to]);
        return $pdf->stream('my.pdf', array('Attachment' => 0));
    }

网络

Route::get('print', 'reportController@print/{from}/{to}')->name('Reports.reportPDF');

观看次数

<a href="{{ route('Reports.reportPDF',[$from,$to]) }}" target="_blank"><button class="btn btn-success" title="Download PDF" data-toggle="tooltip"><i class="fa fa-`download"></i></button></a>

【讨论】:

  • 嘿,我收到一个错误方法 App\Http\Controllers\reportController::print/{from}/{to} 不存在
  • 我也修改了你的代码 Route::get('/print2', 'reportController@print/{from}/{to}')->name('Reports.reportPDF,[$从,$到]');
  • 这是点击打印按钮''localhost/print2/%7B$from%7D/%7B$to%7D''后的链接
  • 如果你把方法名改成print2,就不需要给路由名传递变量了。而不是 name('Reports.reportPDF,[$from,$to]');您可以使用名称('Reports.reportPDF2')。看起来其他人都很好。但是,如果您以适当的方式完成所有操作,则您的 url 不应该是那样的。它应该通过 url 传递正确的值。
【解决方案2】:

我已经使用 Session 实现了这一点。非常感谢:)

Report.blade.php

@php(session(['to' =>  $to, date('Y-m-d')]))
@php(session(['from' =>  $from, date('Y-m-d')]))

PDF.blade.php

Showing Report From:
{{ Carbon\Carbon::parse(request()->session()->get('from'))->formatLocalized('%d %b %Y') }}
To:
{{ Carbon\Carbon::parse(request()->session()->get('to'))->formatLocalized('%d %b %Y') }}

【讨论】:

    【解决方案3】:

    嗯,问题是你做得不太对。

    有一些选择:

    • 使动作print接受参数。

    • 使操作index 接受参数和标志的参数 用于下载(如果有参数,那么您的响应将是 PDF 下载)

    • 像以前一样使用会话。

    对我来说,合适的方法是让print 动作接受参数。 使用 session 有点晦涩难懂,很难维护。

    所以回顾一下,这是我推荐的:

    • 带有日期范围过滤器的操作 indexview

    • 带有日期范围过滤器的操作download。 (基本上相同的输入 作为view 操作)。当然,当单击按钮时,您将使用 JavaScript 收集当前显示给用户的日期范围输入,然后对 download 操作进行 AJAX 调用。

    这是最简洁的解决方案,避免了与会话的紧密耦合,并且更具可读性和可理解性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-11-09
      • 2015-10-23
      • 2015-10-27
      • 1970-01-01
      • 1970-01-01
      • 2017-05-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多