PDF in Laravel returning selected View

Asked

Viewed 559 times

0

Hello, I am using Dompdf to try to return a pdf report, but every time I call the view it is loading as if it were in an infinite loop and nothing happens. Follows my code Cursocontroller.php

public function downloadPDF($id)
    {
      $curso = Curso::find($id);
      $alunos = $curso->alunos;
      $pdf = PDF::loadView('curso.pdf', compact('curso', 'alunos'));
      return $pdf->download('invoice.pdf');

    }

My route is calling so:

Route::get('/cursos/{id}/pdf','CursoController@downloadPDF');

And the view I want to call is pdf.blade.php

@extends('master')
@section('content')


<div class="container">

<h2>{{$curso->nome}}</h2>

    <br>
    <div class="pull-right">
    </div>
    <table class="table table-striped">
    <thead>
      <tr>
        <th>ID</th>
        <th>Nome</th>
        <th>Nascimento</th>
        <th>logradouro</th>
        <th>No</th>
        <th>Bairro</th>
        <th>Cidade</th>
        <th>Estado</th>
        <th>Dt. Criação</th>
      </tr>
    </thead>
    <tbody>
      @foreach($alunos as $value)
      <tr>
        <td>{{$value->id}}</td>
        <td>{{$value->nome}}</td>
        <td>{{$value->data_nascimento}}</td>
        <td>{{$value->logradouro}}</td>
        <td>{{$value->numero}}</td>
        <td>{{$value->bairro}}</td>
        <td>{{$value->cidade}}</td>
        <td>{{$value->estado}}</td>
        <td>{{ date( 'd/m/Y' , strtotime($value->created_at))}}</td>

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


@endsection

I managed to generate the pdf by putting only one

PDF GENERATED

, now I need to make him return the variables.

1 answer

1

What may be happening, is some variable that is used in your master is hindering the printing of pdf.

tries to remove the template, if it continues, tries to debug the controller called by call, so you would have a certainty where the error is happening.

To debug the view, comment blocks and see if any pdf comes out.

  • The master has no variable, it’s just the html start structure

  • Comment on everything and leave only an H1 with any text and see if it generated text, use the same code to print

  • I managed to make him generate the pdf with nothing in the view, the way the controller is, I’ll edit to show the controller, see if you can help me, he has a relationship and I need to call all the items

  • Look, I managed to return the name of the course, but I’m not managing to return the students

  • Is the student relationship with the course correct? sometimes it is common to forget about Return before $this->hasMany() Remembering that along with find($id) you can use the with $course = Course::find($id)->with("students"); and in the view use $course->students for foreach

  • Tá correntinha, to returning on the index

  • give a look at this link I made some considerations on relationship [https://answall.com/questions/286688/retonar-varios-itens-de-uma-rela%C3%A7%C3%a3o-Laravel/286791#286791]

  • a good practice in foreach is to use the $students the $student (to facilitate guidance of what object you are using

Show 3 more comments

Browser other questions tagged

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