Error generating PDF, Undefined variable: o (View:

-4
  metodo gera pdf: 
  ordemController

 $ordens = Ordem::all();
 return \PDF::loadView('ordem.detalhes', compact('ordens'))
->setPaper('a4', 'landscape')
 ->stream('export.pdf');


  view
  <h1 class="text-warning">Ordem numero: {{$o->cliente_id}}</h1>

 <ul>
 <li>
    <b>Cliente:</b>  {{ $o->cliente->nome}}
</li>

<li>
    <b>Telefone:</b>  {{ $o->cliente->telefone}}
</li>

<li>
    <b>Data:</b> {{ $o->data }}
</li>

<li>
    <b>Problema:</b> {{ $o->problema }}
</li>

<li>
    <b>Valor :</b> R$ {{ $o->valor }}
</li>

<li>
    <b>Produto incluso :</b>  {{ $o->produto->nome}}
</li>

<li>
    <b>Tipo de servico :</b>  {{ $o->servico->nome}}
</li>

view list:

 <div class="container-fluid">
  <table class="table table-condensed table-striped table-bordered
  table-hover"> 
   <tr>
    <th>Nome</th>   
    <th> Descrição</th>
    <th class="text-right"> Valor entrada </th>
    <th class="text-right"> Valor saída</th>
    <th class="text-right"> Qtd. estoque </th>
    <th class="text-center">Opções</th>

     </tr>

@foreach($produtos as $p)
<tr class="{{$p->qtd_estoque <=2?'danger':''}}">
    <td>{{$p->nome}}</td>   
    <td>{{$p->descricao}}</td>
    <td class="text-right">{{$p->valor_entrada}}</td>
    <td class="text-right">{{$p->valor_saida}}</td>
    <td class="text-right">{{$p->qtd_estoque}}</td>
    <td class="text-center">

        <div class="btn-group" role="group">
        <a href="/ordem/detalhes/ <?=$o->id ?>" class="btn btn-info btn-sm">
            <span class="glyphicon glyphicon-eye-open"></span>
        </a>
    
asked by anonymous 06.12.2018 / 03:14

1 answer

0

Note that the warning informs you that the "$ o" variable is undefined. That is, it does not exist.

This warning is displayed every time you try to use a variable that does not exist.

And in your code, note that in the following snippet, you define that the variable to be passed to the view is "orders" .

In the following excerpt:

 loadView('ordem.detalhes', compact('ordens'))

And in the view you call the "$ o" variable that does not exist.

Change the $ o variable called in the view by the variable $ orders that was passed to view.

And to check for the existence of a variable before using it, the isset() function is used.

Example:

if( isset($variavel) ){
   // faz alguma coisa
}

Function reference isset()

link

    
28.12.2018 / 14:29