Blade 到 PDF 传递变量

Blade to PDF passing variables

我有这个 blade 视图可以从控制器检索数据。到目前为止,一切都很好。现在我需要将此视图导出为 PDF,但无法传递任何变量。

让我们看一些代码:

我的控制器:

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());
    }

我的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

print_r 结果:

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] => y$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] => y$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

错误:

Undefined variable: contrato (View: /Users/marcellopato/Sites/primorossi/resources/views/contemplado/notaPromissoria.blade.php)

如您所见,我想将这些类型的数据传递给 PDF。传递给查看时效果很好,但我无法导出为 PDF。

有什么提示吗? 提前致谢!

您没有传递关联数组,其中的键是您希望在视图中命名的变量:

PDF::loadView(..., [
    'pessoa' => $pessoa,
    'contrato' => $contrato,
    'endereco' => $endereco,
    'consorciado' => $consorciado,
]);

或使用 compact:

PDF::loadView(..., compact('pessoa', 'contrato', 'endereco', 'consorciado'));

根据您目前的情况,即使是常规视图,您也必须访问 $data 中的所有变量:$data[0]$data[1] 等,因为它们没有命名,因为您有一个零索引数组,而不是关联数组。您正在传递这样的数组:

['data' => [$pessoa, $contrator, ...]]

通过在传递变量后导出视图然后应用 CSS!

解决了该问题