Return relationship data and generate PDF in Laravel / DomPdf

1

Hello, I have a problem and I found a part of the solution here, I'm dealing with this in a different way in another question, however I decided to test a form I saw here from the user Vigilio about DomPDF My case is as follows, I have course and students, I need to return all students enrolled in a PDF course, I can return normally, but when I try to return in pdf it says that an argument is missing, follow the codes

PdfviewController.php

<?php namespace App\Http\Controllers;

use App\Curso;
use App\Aluno;
use Barryvdh\DomPDF\Facade as PDF;

class PdfviewController extends Controller
{

    private $model;
    public function __construct(Curso $model)
    {
        $this->model = $model;
    }

    public function index($id)
    {       
            $data['model'] = $this->model->findOrFail($id);
            $alunos = $curso->alunos;
        return PDF::loadView('viewpdf.index', $data, $alunos)->stream();
    }
}

Route

Route::get('/viewpdf', 'PdfviewController@index');

index.blade.php

<!DOCTYPE html>
<html lang="{{ config('app.locale') }}">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Laravel</title>
    <!-- Fonts -->
    <link href="https://fonts.googleapis.com/css?family=Raleway:100,600" rel="stylesheet" type="text/css">
</head>
<body>
<div class="flex-center position-ref full-height">
    <table>
        <thead>
            <tr>
                <th>Id</th>
                <th>Curso</th>

            </tr>
        </thead>
        <tbody>
            @foreach($alunos as $item)
            <tr>
                <td>{{$item->id}}</td>
                <td>{{$item->nome}}</td>

            </tr>
            @endforeach
        </tbody>
    </table>
</div>
</body>
</html>

I'm wondering if I should pass the ID on the route in this case, but every time I try the result is the same

(1/1) ErrorException Missing argument 1 for App \ Http \ Controllers \ PdfviewController :: index ()

    
asked by anonymous 27.03.2018 / 18:35

2 answers

0

The route should be

Route::get('/{id}/viewpdf', 'PdfviewController@index');

If the parameter was optional, you would still have to use

Route::get('/{id?}/viewpdf', 'PdfviewController@index');
    
28.03.2018 / 13:43
0

Try the following, I think it will solve your problem:

return PDF::loadView('viewpdf.index', compact('data', 'alunos'))->stream();
    
29.09.2018 / 04:11