barryvdh / laravel-dompdf

A DOMPDF Wrapper for Laravel
MIT License
6.62k stars 965 forks source link

undefined data in blade view #984

Open aragornq opened 1 year ago

aragornq commented 1 year ago

$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')

@foreach($data as $i=> $user) @endforeach
# First Name Last Name UserName Grade Father Name
{{$i+1}} {{$user['name']}} {{$user['last_name']}} {{$user['username']}} {{$user['grade']}} {{$user['father_name']}}

@endsection

parallels999 commented 1 year ago

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

AaEzha commented 11 months ago

that should be

$data = User::all();
$pdf = Pdf::loadView('users/pdf', compact('data'));
tigetp commented 10 months ago

I had the same problem, this works for me:

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