php - Pass variables from one function to another in the same Controller
This is my controller and the function for displaying the view. At the moment I have to repeat all the variables from index to exportPDF. And thus, exporting takes too much time.
class MyController extends Controller {
public function index($id) {
$article = Article::find($id);
return view('articles', compact('article');
}
}
And the other method which actually exports the pdf. I want it to be shorter and more simple like this:
public function exportPDF($id) {
$pdf = PDF::loadView('articles', ['article' => $article]);
return $pdf->setPaper('a4')->setOrientation('portrait')->setOption('margin-top', 0)->download('export-' . $id . '.pdf');
}
How can I pass this$article
variable from index to exportPDF function? Btw. the route in web.php (Laravel 5.3) is set up like:
Route::get('exportPDF/{id}', '[email protected]');
Answer
Solution:
You should use it similar like code below