Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

adamjhn's avatar

Howw to show query results in a pdf file? (show each list item in a page of the pdf)

I have this query:

 public function getRegistrationInfo($regID){

        $registration = Registration::with('conference',Conference.registrationTypes','Conference.registrationTypes.participants')
            ->where('id',$regID)->first();

        return view('pdf.registration',compact('registration'));

    }

And in the view "pdf.registration" I have this to show the query results:

 @foreach($registration->conference->registrationTypes as $key=>$registrationType)

        <li>
            <span>show the registration ID here : {{$registration->id}}</span> <br>
            <span>Conference name {{$registration->conference->name}}</span><br>
            <span>Registration type: {{$registration->conference->registrationTypes[$key]['name']}}</span><br>
            <span> Participant: {{$registration->conference->registrationTypes[$key]
            ->participants[0]->name .' '.$registration->
            conference->registrationTypes[$key]->participants[0]->surname}}</span><br>
            <span>Price: {{$registration->conference->registrationTypes[$key]['price']}}</span><br>
        </li>

    @endforeach

So in the view appears a list with two list items showing the info of each participant associated with the registration:

show the registration ID here : 1 

Conference name: conference test title
Registration type: general
Participant: John K
Price: 0
show the registration ID here : 1 


Conference name : conference test ttile
Registration type: plus
Participant: Jake W
Price: 10

Do you know what is necessary to show this results in this list above in a pdf? And each list item in a page of the pdf?

0 likes
7 replies
bobbybouwmann's avatar
Level 88

Well you need to convert your view to a PDF right! There is a really great package for that: https://github.com/barryvdh/laravel-dompdf

You can then do this

public function getRegistrationInfo($regID){
    $registration = Registration::with('conference',Conference.registrationTypes','Conference.registrationTypes.participants')
            ->where('id',$regID)->first();

    $pdf = PDF::loadView('pdf.registration', compact('registration'));

    return $pdf->download('invoice.pdf');
}

This will return the PDF as a download.

1 like
adamjhn's avatar

It appears "Non-static method Barryvdh\DomPDF\PDF::loadView() should not be called statically" on

        $pdf = PDF::loadView('pdf.registration', compact('registration'));

Do you know why?

adamjhn's avatar

I have the aliases:



'aliases' => [

       'App' => Illuminate\Support\Facades\App::class,
       ....
       'PDF' => Barryvdh\DomPDF\Facade::class,
   ],

adamjhn's avatar

Thanks, I had "use Barryvdh\DomPDF\PDF;" with only "use PDF" works.

bobbybouwmann's avatar

If you want the full class name should use this

use Barryvdh\DomPDF\Facade as PDF;
1 like

Please or to participate in this conversation.