PDF传递变量刀片

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PDF传递变量刀片相关的知识,希望对你有一定的参考价值。

我具有从控制器检索数据的刀片视图。到目前为止,一切都很好。现在,我需要将此视图导出为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());
    }

我的刀片:

@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] => AppContrato 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] => IlluminateDatabaseEloquentCollection Object ( [items:protected] => Array ( [0] => AppContrato 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] => AppEndereco 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] => AppUser 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] => TCGVoyagerModelsRole 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] => IlluminateDatabaseEloquentCollection 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, ...]]

以上是关于PDF传递变量刀片的主要内容,如果未能解决你的问题,请参考以下文章

我可以将刀片变量传递给 Laravel 中的 php 变量吗?

如何在控制器传递变量(laravel)中使用刀片进行 foreach

如何从 Laravel5 中的刀片模板传递扩展主模板中的变量?

将变量从刀片传递到路由并从那里传递到另一条路由

将可变表单刀片模板传递给vue组件

在 laravel 刀片路由中使用 JavaScript 变量