laravel-dompdf icon indicating copy to clipboard operation
laravel-dompdf copied to clipboard

undefined data in blade view

Open aragornq opened this issue 2 years ago • 3 comments

$data = User::all()->toArray(); $pdf = Pdf::loadView('users/pdf', $data); return $pdf->download('usersPDF.pdf'); in pdf.blade.php which is the view receives $data you can see error (undefined variable $data) . could you help me about it ?

this is balde file:

@extends('index') @section('content')

   </tr>
    </thead>
    <tbody>

    @foreach($data as $i=> $user)
    <tr class="text-center">
        <td>{{$i+1}}</td>
        <td>{{$user['name']}}</td>
        <td>{{$user['last_name']}}</td>
        <td>{{$user['username']}}</td>
        <td>{{$user['grade']}}</td>
        <td>{{$user['father_name']}}</td>
     

    </tr>

    @endforeach
    </tbody>
</table>

@endsection

# First Name Last Name UserName Grade Father Name

aragornq avatar Jul 12 '23 00:07 aragornq

Read laravel documentation That is not how the data is sent to the views

parallels999 avatar Jul 12 '23 17:07 parallels999

that should be

$data = User::all();
$pdf = Pdf::loadView('users/pdf', compact('data'));

AaEzha avatar Sep 12 '23 20:09 AaEzha

I had the same problem, this works for me:

$pdf = Pdf::loadView('users/pdf', ['data'] => $data);

tigetp avatar Oct 15 '23 00:10 tigetp