I have this blade view that retrieves data from a controller. So far, so good. Now I need to export this view to a PDF, but can't pass any variables.
Let's see some code:
My controller:
public function nota(Request $request)
{
$pessoa = Contrato::where('user_id', '=', Auth::user()->id)->first();
$contrato = Contrato::where('user_id', '=', Auth::user()->id)
->where('disponivel', '=', 1)->get();
$endereco = Endereco::where('user_id', '=', Auth::user()->id)->first();
$consorciado = Auth::user();
$data = [$pessoa, $contrato, $endereco, $consorciado];
$pdf = PDF::loadView('contemplado.notaPromissoria', compact('data'));
return $pdf->download('notapromissoria.pdf');
// return view('contemplado.notaPromissoria')->withPessoa($pessoa)->withContrato($contrato)->withEndereco($endereco)->withConsorciado(Auth::user());
}
My blade:
@extends('contemplado.app')
@section('content')
<div class="container m-t-50">
<div class="container">
<h4>Nota Promissória</h4>
<div class="card">
<div class="card-body">
<div class="row">
<div class="col-sm-6">
{{-- Emitente: <strong>{{ $consorciado->name }}</strong> --}}
</div>
<div class="col-sm-6">
Contrato número:
@foreach($contrato as $c)
<strong>{{ $c->contrato }}</strong>
@endforeach
</div>
</div>
@foreach($contrato as $c)
<div class="row">
<div class="col-sm-6">Percentual Vincendo: <strong>{{ $c->percQuitacao }} %</strong></div>
<div class="col-sm-6">Grupo: <strong>{{ $c->grupo }}</strong>, Cota: <strong>{{ $c->cota }}</strong></div>
</div>
@endforeach
@foreach($contrato as $c)
Valor: <strong>R$ {{ $c->valor }}</strong><br>
<div class="row">
<div class="col-sm-4">
Aos dias
<hr class="m-t-10 new3">
</div>
<div class="col-sm-4">
do mês de
<hr class="m-t-10 new3">
</div>
<div class="col-sm-4">
de
<hr class="m-t-10 new3">
</div>
</div>
<div class="row m-t-10">
<div class="col-sm-12">
, pagarei por esta <strong>NOTA PROMISSÓRIA</strong> ao <strong>PRIMO ROSSI ADM DE CONSÓRCIO LTDA</strong> ou à sua ordem a quantia de:<small><i>(por extenso)</i></small>
<hr class="m-t-10 new3"> em moeda corrente, no valor correspondente ao saldo devedor a data de:{DATA DO DIA DA EMISSÃO}
</div>
</div>
@endforeach
<hr>
<div class="row m-b-20">
<div class="col-sm-12">
<strong>Esta Nota Promissória é inegociável, conforme contrato de alienação fiduciária e/ou escritura pública de confissão de dívida com garantia hipotecária, a qual está vinculada, fazendo parte integrante e inseparável dos mesmos</strong>
</div>
</div>
</div>
<div class="card-body">
<div class="row">
<div class="col-sm-6">
{{-- Nome: <strong>{{ $consorciado->name }}</strong> --}}
</div>
<div class="col-sm-6">
{{-- CPF/CNPJ: <strong>{{ $consorciado->cpfCnpj }}</strong> --}}
</div>
</div>
<div class="row">
<div class="col-sm-12">
Endereço: <strong>{{ $endereco->logradouro }}</strong>, <strong>{{ $endereco->numero }}</strong>
</div>
</div>
<div class="row">
<div class="col-sm-6">
Cidade: <strong>{{ $endereco->municipio }}</strong>
</div>
<div class="col-sm-6">
Estado: <strong>{{ $endereco->uf }}</strong>
</div>
</div>
<div class="row">
<div class="col-sm-6">
CEP: <strong>{{ $endereco->cep }}</strong>
</div>
<div class="col-sm-6">
Telefones: <strong>{{ $endereco->telefone }}</strong> <strong>{{ $endereco->celular }}</strong>
</div>
</div>
<div class="row">
<div class="col-sm-12">
Assinatura Emitente:<br>
<hr class="m-t-10 new3">
</div>
</div>
</div>
<div class="card-body">
<div class="card-tile">
<strong>Primeiro Avalista</strong>
</div>
<div class="row">
<div class="col-sm-6">
Nome:
<hr class="m-t-10 new3">
</div>
<div class="col-sm-6">
CPF:
<hr class="m-t-10 new3">
</div>
</div>
<div class="row">
<div class="col-sm-12">
Endereço:
<hr class="m-t-10 new3">
</div>
</div>
<div class="row">
<div class="col-sm-6">
Cidade:
<hr class="m-t-10 new3">
</div>
<div class="col-sm-6">
Estado:
<hr class="m-t-10 new3">
</div>
</div>
<div class="row">
<div class="col-sm-6">
CEP:
<hr class="m-t-10 new3">
</div>
<div class="col-sm-6">
Telefones:
<hr class="m-t-10 new3">
</div>
</div>
<div class="row">
<div class="col-sm-12">
Assinatura do Avalista:<br>
<hr class="m-t-10 new3">
</div>
</div>
<div class="row">
<div class="col-sm-6">
Nome do Cônjuge:
<hr class="m-t-10 new3">
</div>
<div class="col-sm-6">
CPF do Cônjuge:
<hr class="m-t-10 new3">
</div>
</div>
<div class="row m-t-10">
<div class="col-sm-12">
Assinatura do Cônjuge do Avalista:<br>
<hr class="m-t-10 new3">
</div>
</div>
<hr>
<div class="card-tile">
<strong>Segundo Avalista</strong>
</div>
<div class="row">
<div class="col-sm-6">
Nome:
<hr class="m-t-10 new3">
</div>
<div class="col-sm-6">
CPF:
<hr class="m-t-10 new3">
</div>
</div>
<div class="row">
<div class="col-sm-12">
Endereço:
<hr class="m-t-10 new3">
</div>
</div>
<div class="row">
<div class="col-sm-6">
Cidade:
<hr class="m-t-10 new3">
</div>
<div class="col-sm-6">
Estado:
<hr class="m-t-10 new3">
</div>
</div>
<div class="row">
<div class="col-sm-6">
CEP:
<hr class="m-t-10 new3">
</div>
<div class="col-sm-6">
Telefones:
<hr class="m-t-10 new3">
</div>
</div>
<div class="row">
<div class="col-sm-12">
Assinatura do Avalista:<br>
<hr class="m-t-10 new3">
</div>
</div>
<div class="row">
<div class="col-sm-6">
Nome do Cônjuge:
<hr class="m-t-10 new3">
</div>
<div class="col-sm-6">
CPF do Cônjuge:
<hr class="m-t-10 new3">
</div>
</div>
<div class="row m-t-10">
<div class="col-sm-12">
Assinatura do Cônjuge do Avalista:<br>
<hr class="m-t-10 new3">
</div>
</div>
</div>
</div>
</div>
</div>
@endsection
@section('scripts')
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script>
var vue = new Vue({
el: '#app',
data: {
isLoading: false
},
});
</script>
@endsection
The print_r result:
Array ( [0] => App\Contrato Object ( [guarded:protected] => Array ( ) [connection:protected] => mysql [table:protected] => contratos [primaryKey:protected] => id [keyType:protected] => int [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [id] => 459 [user_id] => 842 [contrato] => 851717 [disponivel] => 1 [grupo] => 06271 [cota] => 871 [valor] => 34163 [conta_conjunta] => [novoSegmento] => [originalSegmento] => 06271 [completo] => [novoUsado] => 1 [pessoa] => F [descricao] => 50% COBALT 1.8 (COD.9062) [data_entrega] => 1970-01-01 00:00:01 [data_contemplacao] => 2019-11-12 00:00:00 [cep] => 38407526 [codFormaPagto] => 3 [codigo_bem] => 9075 [codPlanoCota] => 70 [data_cancelamento] => 1970-01-01 00:00:01 [dataProxReuniao] => 2020-01-14 00:00:00 [descSituacaoCobranca] => Normal [faseCobranca] => N000 [numSeq] => 0 [percQuitacao] => 26.5461 [valorQuitacao] => 10505.54 [percTotalPago] => 73.4539 [percTotalPendente] => 0 [situacaoCobranca] => N [garagem] => [created_at] => 2019-12-19 12:55:07 [updated_at] => 2019-12-19 12:55:21 ) [original:protected] => Array ( [id] => 459 [user_id] => 842 [contrato] => 851717 [disponivel] => 1 [grupo] => 06271 [cota] => 871 [valor] => 34163 [conta_conjunta] => [novoSegmento] => [originalSegmento] => 06271 [completo] => [novoUsado] => 1 [pessoa] => F [descricao] => 50% COBALT 1.8 (COD.9062) [data_entrega] => 1970-01-01 00:00:01 [data_contemplacao] => 2019-11-12 00:00:00 [cep] => 38407526 [codFormaPagto] => 3 [codigo_bem] => 9075 [codPlanoCota] => 70 [data_cancelamento] => 1970-01-01 00:00:01 [dataProxReuniao] => 2020-01-14 00:00:00 [descSituacaoCobranca] => Normal [faseCobranca] => N000 [numSeq] => 0 [percQuitacao] => 26.5461 [valorQuitacao] => 10505.54 [percTotalPago] => 73.4539 [percTotalPendente] => 0 [situacaoCobranca] => N [garagem] => [created_at] => 2019-12-19 12:55:07 [updated_at] => 2019-12-19 12:55:21 ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [timestamps] => 1 [hidden:protected] => Array ( ) [visible:protected] => Array ( ) [fillable:protected] => Array ( ) ) [1] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( [0] => App\Contrato Object ( [guarded:protected] => Array ( ) [connection:protected] => mysql [table:protected] => contratos [primaryKey:protected] => id [keyType:protected] => int [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [id] => 459 [user_id] => 842 [contrato] => 851717 [disponivel] => 1 [grupo] => 06271 [cota] => 871 [valor] => 34163 [conta_conjunta] => [novoSegmento] => [originalSegmento] => 06271 [completo] => [novoUsado] => 1 [pessoa] => F [descricao] => 50% COBALT 1.8 (COD.9062) [data_entrega] => 1970-01-01 00:00:01 [data_contemplacao] => 2019-11-12 00:00:00 [cep] => 38407526 [codFormaPagto] => 3 [codigo_bem] => 9075 [codPlanoCota] => 70 [data_cancelamento] => 1970-01-01 00:00:01 [dataProxReuniao] => 2020-01-14 00:00:00 [descSituacaoCobranca] => Normal [faseCobranca] => N000 [numSeq] => 0 [percQuitacao] => 26.5461 [valorQuitacao] => 10505.54 [percTotalPago] => 73.4539 [percTotalPendente] => 0 [situacaoCobranca] => N [garagem] => [created_at] => 2019-12-19 12:55:07 [updated_at] => 2019-12-19 12:55:21 ) [original:protected] => Array ( [id] => 459 [user_id] => 842 [contrato] => 851717 [disponivel] => 1 [grupo] => 06271 [cota] => 871 [valor] => 34163 [conta_conjunta] => [novoSegmento] => [originalSegmento] => 06271 [completo] => [novoUsado] => 1 [pessoa] => F [descricao] => 50% COBALT 1.8 (COD.9062) [data_entrega] => 1970-01-01 00:00:01 [data_contemplacao] => 2019-11-12 00:00:00 [cep] => 38407526 [codFormaPagto] => 3 [codigo_bem] => 9075 [codPlanoCota] => 70 [data_cancelamento] => 1970-01-01 00:00:01 [dataProxReuniao] => 2020-01-14 00:00:00 [descSituacaoCobranca] => Normal [faseCobranca] => N000 [numSeq] => 0 [percQuitacao] => 26.5461 [valorQuitacao] => 10505.54 [percTotalPago] => 73.4539 [percTotalPendente] => 0 [situacaoCobranca] => N [garagem] => [created_at] => 2019-12-19 12:55:07 [updated_at] => 2019-12-19 12:55:21 ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [timestamps] => 1 [hidden:protected] => Array ( ) [visible:protected] => Array ( ) [fillable:protected] => Array ( ) ) ) ) [2] => App\Endereco Object ( [connection:protected] => mysql [table:protected] => enderecos [primaryKey:protected] => id [keyType:protected] => int [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [id] => 395 [user_id] => 842 [logradouro] => R DA REALIZAÇÃO [numero] => 760 [bairro] => JOANA DARC [municipio] => [uf] => MG [complemento] => [telefone] => (34) 9885-1104 [celular] => (11) 91234-5678 [cep] => 38407-526 [correspondencia] => [tempoResidencia] => 10 [created_at] => 2019-12-19 12:55:12 [updated_at] => 2019-12-19 13:03:50 ) [original:protected] => Array ( [id] => 395 [user_id] => 842 [logradouro] => R DA REALIZAÇÃO [numero] => 760 [bairro] => JOANA DARC [municipio] => [uf] => MG [complemento] => [telefone] => (34) 9885-1104 [celular] => (11) 91234-5678 [cep] => 38407-526 [correspondencia] => [tempoResidencia] => 10 [created_at] => 2019-12-19 12:55:12 [updated_at] => 2019-12-19 13:03:50 ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [timestamps] => 1 [hidden:protected] => Array ( ) [visible:protected] => Array ( ) [fillable:protected] => Array ( ) [guarded:protected] => Array ( [0] => * ) ) [3] => App\User Object ( [fillable:protected] => Array ( [0] => name [1] => cpfCnpj [2] => password ) [hidden:protected] => Array ( [0] => password [1] => remember_token ) [casts:protected] => Array ( [email_verified_at] => datetime ) [guarded:protected] => Array ( ) [additional_attributes] => Array ( [0] => locale ) [connection:protected] => mysql [table:protected] => users [primaryKey:protected] => id [keyType:protected] => int [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [id] => 842 [role_id] => 2 [fiador_id] => [name] => JOSE CARLOS SOARES [email] => email@pwa.com [avatar] => users/default.png [email_verified_at] => [password] => $2y$10$EqgjoWwYe2FBLDkJALqUm.kNLQCjovdEuRrtIhOfEV9lvqM2LxP8G [remember_token] => [settings] => [cc] => [unidade] => [matricula] => [departamento] => [equipe] => [cep] => [cargo] => [uf] => [telefoneComercial] => [celular_comercial] => [telefone] => [nascimento] => [seriePagSeguro] => [senhaMaquininha] => [numeroArmario] => [precisaAssEletronica] => [whatsapp] => [novaAssinatura] => [cpfCnpj] => 00445341670 [IE] => [estado_civil_id] => 1 [renda] => 2 [tipoBeneficio] => [numeroBeneficio] => [tempoBeneficio] => [atividade_principal] => [atividades_secundarias] => [capital_social] => [data_situacao] => [data_situacao_especial] => [fantasia] => [motivo_situacao] => [natureza_juridica] => [porte] => [status] => [tipo] => [ultima_atualizacao] => [receita] => [idERP] => 1 [sms] => [adesao] => [created_at] => 2019-12-19 12:55:07 [updated_at] => 2019-12-19 13:59:20 ) [original:protected] => Array ( [id] => 842 [role_id] => 2 [fiador_id] => [name] => JOSE CARLOS SOARES [email] => email@pwa.com [avatar] => users/default.png [email_verified_at] => [password] => $2y$10$EqgjoWwYe2FBLDkJALqUm.kNLQCjovdEuRrtIhOfEV9lvqM2LxP8G [remember_token] => [settings] => [cc] => [unidade] => [matricula] => [departamento] => [equipe] => [cep] => [cargo] => [uf] => [telefoneComercial] => [celular_comercial] => [telefone] => [nascimento] => [seriePagSeguro] => [senhaMaquininha] => [numeroArmario] => [precisaAssEletronica] => [whatsapp] => [novaAssinatura] => [cpfCnpj] => 00445341670 [IE] => [estado_civil_id] => 1 [renda] => 2 [tipoBeneficio] => [numeroBeneficio] => [tempoBeneficio] => [atividade_principal] => [atividades_secundarias] => [capital_social] => [data_situacao] => [data_situacao_especial] => [fantasia] => [motivo_situacao] => [natureza_juridica] => [porte] => [status] => [tipo] => [ultima_atualizacao] => [receita] => [idERP] => 1 [sms] => [adesao] => [created_at] => 2019-12-19 12:55:07 [updated_at] => 2019-12-19 13:59:20 ) [changes:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( [role] => TCG\Voyager\Models\Role Object ( [guarded:protected] => Array ( ) [connection:protected] => mysql [table:protected] => roles [primaryKey:protected] => id [keyType:protected] => int [incrementing] => 1 [with:protected] => Array ( ) [withCount:protected] => Array ( ) [perPage:protected] => 15 [exists] => 1 [wasRecentlyCreated] => [attributes:protected] => Array ( [id] => 2 [name] => user [display_name] => Normal User [created_at] => 2019-06-10 14:53:44 [updated_at] => 2019-06-10 14:53:44 ) [original:protected] => Array ( [id] => 2 [name] => user [display_name] => Normal User [created_at] => 2019-06-10 14:53:44 [updated_at] => 2019-06-10 14:53:44 ) [changes:protected] => Array ( ) [casts:protected] => Array ( ) [dates:protected] => Array ( ) [dateFormat:protected] => [appends:protected] => Array ( ) [dispatchesEvents:protected] => Array ( ) [observables:protected] => Array ( ) [relations:protected] => Array ( ) [touches:protected] => Array ( ) [timestamps] => 1 [hidden:protected] => Array ( ) [visible:protected] => Array ( ) [fillable:protected] => Array ( ) ) [roles] => Illuminate\Database\Eloquent\Collection Object ( [items:protected] => Array ( ) ) ) [touches:protected] => Array ( ) [timestamps] => 1 [visible:protected] => Array ( ) [rememberTokenName:protected] => remember_token ) ) 1
The error:
Undefined variable: contrato (View: /Users/marcellopato/Sites/primorossi/resources/views/contemplado/notaPromissoria.blade.php)
As you can see, I want to pass those types of data to the PDF. It works well when passing to view, but I can't do the export to PDF.
Any hints? Thanks in advance!
You are not passing an associative array where the keys are what you want the variables to be named in the view:
PDF::loadView(..., [
'pessoa' => $pessoa,
'contrato' => $contrato,
'endereco' => $endereco,
'consorciado' => $consorciado,
]);
OR using compact
:
PDF::loadView(..., compact('pessoa', 'contrato', 'endereco', 'consorciado'));
With what you currently have, even if it was a regular view, you would have to access all the variables from $data
: $data[0]
, $data[1]
, etc., as they are not named because you have a zero index array, not an associative array. You are passing an array like so:
['data' => [$pessoa, $contrator, ...]]