Return relationship data and generate PDF in Laravel/Dompdf

Asked

Viewed 714 times

2

Hello, I have a problem and I found a part of the solution here, I am dealing with it in a different way in another question, but I decided to test a way that I saw here of the user Vigilio on Dompdf My case is the following, I have course and students, I need to return all registered students in a PDF course, I can return normally, but when I try to return in pdf it says that it lacks an argument, follows 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 by 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()

2 answers

1

The route must be

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

If the parameter was optional you would still have to use

Route::get('/{id?}/viewpdf', 'PdfviewController@index');
  • I’ll test it right away, but in the controller, it’s the same?

  • Yes, the controller is the same, normally I n use findOrFail use only find, but serve tbm

0

Try it this way, I think it solves your problem:

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.