简体   繁体   中英

How to use dompdf on Laravel 7 correctly?

I am using barryvdh dompdf for my project, I am also new with laravel framework. I am trying to generate a pdf based on a query that will return one row. The error message is: "Trying to get property 'schedule_date' of non-object".

This is my function in my controller:

public function downloadPAasPDF($id)
    {
        $decoded_id = base64_decode($id);

            $data = DB::select('SELECT
             p.appointment_id, 
             d.schedule_date,
             d.time_start,
             d.time_end,
             o.oncology_name,
             pt.procedure_name
            , CONCAT(u1.first_name," ",u1.last_name) AS doctor, CONCAT(u2.first_name," ",u2.last_name) AS patient FROM doctor_schedule d
            LEFT JOIN patient_appointment p ON d.schedule_id = p.schedule_id
            LEFT JOIN users u1 ON u1.id = d.user_id
            LEFT JOIN users u2 ON u2.id = p.user_id
            LEFT JOIN oncology o ON p.oncology_id = o.oncology_id
            LEFT JOIN procedure_type pt ON p.procedure_id = pt.procedure_id
            WHERE p.appointment_id = '.$decoded_id.';');

            $pdf = PDF::loadView('Appointments.PatientAppointmentPDF', ['data' => $data]);
            return $pdf->download('Appointment.pdf');
    }

And this is my view:

 <table class="table">
                    <thead>
                        <tr>
                        <th style="font-size:21px;">Reference No. {{preg_replace('/[^A-Za-z0-9. ]/', '',$data->schedule_date)}}{{$data->appointment_id}} </th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr>
                            <td style="font-size:23px;width:606px;height:46px;">Patient Name: {{$data->patient}}</td>
                            <td>Date: {{ date_format(date_create($data->schedule_date), 'm-d-Y') }}</td>
                        </tr>
                        <tr style="width:1050px;">
                            <td style="width:345px;">Time Start: {{ date('g:i A',strtotime($data->time_start)) }}</td>
                            <td>Time End: {{date('g:i A',strtotime($data->time_end))}}</td>
                        </tr>
                        <tr style="width:1050px;height:-118px;">
                            <td style="width:321px;height:98px;">Oncology: {{$data->oncology_name}}</td>
                            <td>Procedure: {{$data->procedure_name}}</td>
                        </tr>
                        <tr style="width:1050px;height:-118px;">
                            <td style="width:321px;height:98px;font-size:20px;">Doctor: {{$data->doctor}}</td>
                        </tr>
                    </tbody>
                </table>

Here there are several things to keep in mind, the problem is possible that the query is not returning a result, first you should query directly by inserting that statement in the database manager (navicat or postgresql or phpmyadmin) to avoid errors in the query and verify that returns some tuple, second, you should somewhere in the code verify if the query returns any result, that is, if $data is not null or if it exists for validation reasons in your system and if there are no results, notify the user, on the other side I don't understand why you decode from base64? I imagine that the parameter is base64 encoded but I don't know why? any security reason? although occultism is not security... but it could be that in the decoding process you did not obtain the expected result... last but not least I do not remember if it is necessary in the query to add the; in the case of this type of query with query builder... PD: You could use phpstorm and install the debug to follow the process step by step. But I guess the problem is in the query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM