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
The master has no variable, it’s just the html start structure
– Linton Junior
Comment on everything and leave only an H1 with any text and see if it generated text, use the same code to print
– Lucas Hart
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
– Linton Junior
Look, I managed to return the name of the course, but I’m not managing to return the students
– Linton Junior
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
– Lucas Hart
Tá correntinha, to returning on the index
– Linton Junior
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]
– Lucas Hart
a good practice in foreach is to use the $students the $student (to facilitate guidance of what object you are using
– Lucas Hart